未分類

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

matplotlib で x, y軸・タイトルのフォントサイズを調整する

ラベルの fontsize で指定する。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2, 100) …

no image

python3 で文字列のリストを結合して1つの文字列にする方法

リストに対して、join メソッドを実行する。 例 list1 = [‘hello, ‘,’good ‘, ‘morning’] str1 = ”.join(list1) print(str1) 結 …

no image

tex と分数

tex で分数を入力するには、frac を使います。 \frac{2}{3} 表示結果 次のように表示されてしまう場合は、バッククォート(\ マーク)が抜けているのが原因です。 間違った場合。

no image

tex で同値記号(⇔)を入力する方法

同値記号を入力するには、\Leftrightarrow を使う。 例 $b \leftrightarrow c$, $b \Leftrightarrow c$

no image

tex でかけ算の記号 x (✕)を表示する

かけ算記号を表示するには、$\times$ を使う。 例 $2 \times 3$ 結果