2017-05-18 11 views
0

私はここで解決策を検討してきましたが、Pythonに初めて挑戦しています。私の問題は解決するのは簡単だと思うが、どんな助けも素晴らしいだろう。Pythonでテキストファイルの1行を読み込んで編集する

私はそれで次のいたテキストファイルがある場合:私は、私は、Pythonのname2データを表示する方法を10を言うために43からName2は第二の数を交換したい知っている

Name1, 12, 32 
Name2, 49, 43 
Name3, 43, 13 
Name4, 43, 53 

を、それを編集してテキストファイルに戻しますか?

また、PythonでName2というテキストファイルのどの行がオンになっているかを知ることができます(たとえば、2行目)。

お願いします - 最も簡単な解決策!ありがとうございました 。 Name1が表示される場所

+0

http://www.pythonforbeginners.com/files/reading-and-writing-files:

import csv with open("path/to/file") as infile: for i, (name, *data) in enumerate(csv.reader(infile), 1): if name != "Name1": continue print("'Name1' found on line", i) 

それに関連するデータを交換します-in-python – Dadep

答えて

0

は、印刷するには:

with open("path/to/file") as infile, open("path/to/output", 'w') fout: 
    outfile = csv.writer(fout) 
    for i, (name, first, second) in enumerate(csv.reader(infile), 1): 
     if name == "Name1": 
      second -= 33 
     outfile.writerow([name, first, second]) 
関連する問題