未分類

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 と分数

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

no image

python3 で文字列を区切り文字を指定してリストに変換する方法

split メソッドを使う。 例 str1 = ‘this, is, a, pen’ str2 = str1.split(‘,’) print(str2) 結果 [‘this’, ‘ is’, ‘ a …

no image

tex で文字色を RGB で指定する

文字色を RGB で指定する。例では、\definecolor で、「samplecolor」という色を定義して使っている。 例 \usepackage{xcolor}\begin{document} …

no image

c++ で文字列(string)の長さを取得する方法

length() で取得する。 例 #include <iostream> #include <string> using namespace std; int main () { str …

no image

tex で三角形の合同記号を入力する

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