未分類

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

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

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

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

python3 でカンマの入った数値の文字列を数値に変換する方法

replace と int 変換を組み合わせる。 例 str1 = ‘1,234,567’ int1 = int(str1.replace(‘,’,”)) print(int1) 結果 123456 …

no image

tex で行内を結合する方法

\multicolumn を使って、結合する行の成分の数を指定することができる。次の例では「3」を指定して、結合している。 例 \begin{table} \begin{tabular}{ |c|c| …