300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python读取文本并且替换_lin如何读取和替换python行文件中的文本

python读取文本并且替换_lin如何读取和替换python行文件中的文本

时间:2020-02-09 19:46:34

相关推荐

python读取文本并且替换_lin如何读取和替换python行文件中的文本

我是python的初学者,想学习如何替换来自不同文件的文本。在

我知道如何进行基本操作,但需要帮助:

我有3个文件主.txt,名称.txt和数字.txt

在名称.txt看起来像这样:Anna

Smith

Bob

Jhon

在数字.txt看起来像这样:

^{pr2}$

文件中的第1行名称.txt对应于中的第1行数字.txt(所以安娜的电话是第一个数字.txt,史密斯电话在第二位数字.txt以此类推)

现在问题是:

文件主.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对应的行在数字.txt和名称.txt。

所以编辑的主.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行文本。有人能帮我吗?在

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。