未分類

python3 で特定のデータが含まれる行を削除する

投稿日:

data.txt からの入力から、「4 5 6」というデータがある行を削除して、output.txt に出力する。
(データの各行の最後に \n があることに注意。)





f = open('data.txt','r')
out = open('output.txt','w')
array = []
for line in f:
 if line != '4 5 6\n':
  out.write(line)

入力ファイル

data.txt(入力ファイル)
1 2 3
4 5 6
7 8 9

結果

output.txt (出力ファイル)

1 2 3
7 8 9

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex で文字を上付きにした矢印を入力する方法

\overset を使う方法がある。 例 $\overset{\text{abc}}{\rightarrow}$ 結果

no image

tex で数式モード内に普通の文字列を入力する方法

\textrm を使う。 例 $$y = f (x) \hspace{1cm} \textrm{This is a function.}$$ 結果

no image

matplotlib で x軸・y軸ラベルをつける

plt.xlabel と plt.ylabel を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, …

no image

tex で三角形の相似記号を入力する

\sim を使う。 例 $\triangle{ABC} \sim \triangle{DEF}$ 結果

no image

python3 でリストの偶数番目・奇数番目の項目を出力する

リストに対して、::2 を使えば良い。 例 list1 = [‘日’,’月’,’火’,’水’,’木’,’金’,’土’] list2 = list1[::2] list3 = list1[1::2] p …