ホームページ  >  記事  >  WeChat アプレット  >  Pythonのファイル操作で特定の行を削除する方法を詳しく解説

Pythonのファイル操作で特定の行を削除する方法を詳しく解説

迷茫
迷茫オリジナル
2017-03-25 14:35:522619ブラウズ

この書き込みループをスキップするには、Continue を使用するだけです

#文本内容
Yesterday when I was young
昨日当我年少轻狂
The tasting of life was sweet
生命的滋味是甜的
As rain upon my tongue
tasting
I lived by night and shunned the naked light of day
tasting123
And only now I see how the time ran away
tasting
 
tasting

テキスト内の testing123 を削除してください

with open("fileread.txt","r",encoding="utf-8") as f:
    lines = f.readlines()
    #print(lines)
with open("fileread.txt","w",encoding="utf-8") as f_w:
    for line in lines:
        if "tasting123" in line:
            continue
        f_w.write(line)

以上がPythonのファイル操作で特定の行を削除する方法を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。