未分類

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

c言語で文字をつなげる方法

sprintf 関数を使って、result 変数に文字列 s1 と文字列 s2 を結合した文字列を出力することができる。 例 #include<stdio.h> int main(void) { …

no image

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

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

no image

C言語で文字をアルファベット順にずらす

C言語で文字をずらすには、char 型の変数に1を加えればよい。 例 #include <stdio.h> int main(void) { char c; c = ‘a’; c++; p …

no image

tex でファイ(φ、Φ)を入力する方法

小文字のファイ:φ、大文字のファイ:Φ串刺しでないファイ($\varphi$)も使われることがある。 例 $\phi, \varphi, \Phi$ 結果

no image

c言語 ターミナル上で出力した文字列の表示を初期化する

エスケープシーケンス \033[2J と\033[u を使う。 例 #include <unistd.h> #include <stdio.h> int main() { in …