我是python的初學者,希望學習如何替換來自不同文件的文本。如何在python中逐行讀取和替換文本中的文本
我知道如何做到這一點基本的,但需要這方面的幫助:
我有3個文件main.txt,names.txt中和number.txt
names.txt中看起來是這樣的:
Anna
Smith
Bob
Jhon
number.txt看起來是這樣的:
1-522-223
1-523-232
1-593-573
1-322-242
文件names.txt中1號線與number.txt線1對應(所以安娜的手機是第一個number.txt,史密斯手機在number.txt第二等)
現在這裏有一個問題: 文件main.txt看起來是這樣的:
The person Judy lives in Ontario and has phone number 1-888-2923
The person Michael lives in Toronto and has phone number 1-999-2388
The person Cameron lives in Berlin and has phone number 1-666-2888
The person Douglas lives in Tokyo and has phone number 5-7777-223
我知道如何查找和替換,問題是我需要相應改變的電話和名字在每行中main.txt行numbers.txt and names.txt。 所以編輯main.txt應該是:
The person Anna lives in Ontario and has phone number 1-522-223
The person Smith lives in Toronto and has phone number 1-523-232
The person Bob lives in Berlin and has phone number 1-593-573
and so on...
我真的不知道如何做到這一點,而文件是相當大的,像2000行文字。誰能幫我 ?
你會發布你迄今爲止寫的代碼嗎? – user2314737 2014-09-22 09:52:33
爲什麼三個文件中的信息都是以?開頭的?這很容易失去同步,可能會考慮使用具有不同列中信息的CSV樣式文件。 – Werner 2014-09-22 09:55:14