python读取文本并且替换_lin如何读取和替换python行文件中的文本
我是python的初學(xué)者,想學(xué)習(xí)如何替換來自不同文件的文本。在
我知道如何進(jìn)行基本操作,但需要幫助:
我有3個(gè)文件主.txt,名稱.txt和數(shù)字.txt
在名稱.txt看起來像這樣:Anna
Smith
Bob
Jhon
在數(shù)字.txt看起來像這樣:
^{pr2}$
文件中的第1行名稱.txt對(duì)應(yīng)于中的第1行數(shù)字.txt(所以安娜的電話是第一個(gè)數(shù)字.txt,史密斯電話在第二位數(shù)字.txt以此類推)
現(xiàn)在問題是:
文件主.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
我知道如何查找和替換,問題是我需要更改電話和每一行的名稱主.txt對(duì)應(yīng)的行在數(shù)字.txt和名稱.txt。
所以編輯的主.txt應(yīng)為: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...
我真的不知道怎么做,而且文件相當(dāng)大,像2000行文本。有人能幫我嗎?在
總結(jié)
以上是生活随笔為你收集整理的python读取文本并且替换_lin如何读取和替换python行文件中的文本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fastapi jinja2_(入门篇)
- 下一篇: python怎么使用json_Pytho