Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我尝试在linux中使用f.openPython更改它们时,内容会重复_Python_Linux - Fatal编程技术网

当我尝试在linux中使用f.openPython更改它们时,内容会重复

当我尝试在linux中使用f.openPython更改它们时,内容会重复,python,linux,Python,Linux,我的代码是这种格式的 #!/usr/bin/python fout = open("names2", "w") fin = open("names", "r") for line in fin: fout.write(line.replace('Smith','Mr.Smith')) fout.write(line.replace('Walker','Mr.Walker')) fout.close() 我的示例文件是 Smith Anderson

我的代码是这种格式的

#!/usr/bin/python

fout = open("names2", "w")
fin = open("names", "r")

for line in fin:
   fout.write(line.replace('Smith','Mr.Smith'))
   fout.write(line.replace('Walker','Mr.Walker'))

fout.close()
我的示例文件是

Smith           Anderson        Clark           Wright          Mitchell
Johnson         Thomas          Rodriguez       Lopez           Perez
Williams        Jackson         Lewis           Hill            Roberts
Jones           White           Lee             Scott           Turner
Brown           Harris          Walker          Green           Phillips
Davis           Martin          Hall            Adams           Campbell
Miller          Thompson        Allen           Baker           Parker
Wilson          Garcia          Young           Gonzalez        Evans
Moore           Martinez        Hernandez       Nelson          Edwards
Taylor          Robinson        King            Carter          Collins
我没有让沃克先生把史密斯换成史密斯先生和沃克先生,而是

Mr.Smith                Anderson        Clark           Wright          Mitchell
Smith           Anderson        Clark           Wright          Mitchell
Johnson         Thomas          Rodriguez       Lopez           Perez
Johnson         Thomas          Rodriguez       Lopez           Perez
Williams        Jackson         Lewis           Hill            Roberts
Williams        Jackson         Lewis           Hill            Roberts
Jones           White           Lee             Scott           Turner
Jones           White           Lee             Scott           Turner
Brown           Harris          Walker          Green           Phillips
Brown           Harris          Mr.Walker               Green           Phillips
Davis           Martin          Hall            Adams           Campbell
Davis           Martin          Hall            Adams           Campbell
Miller          Thompson        Allen           Baker           Parker
Miller          Thompson        Allen           Baker           Parker
Wilson          Garcia          Young           Gonzalez        Evans
Wilson          Garcia          Young           Gonzalez        Evans
Moore           Martinez        Hernandez       Nelson          Edwards
Moore           Martinez        Hernandez       Nelson          Edwards
Taylor          Robinson        King            Carter          Collins
Taylor          Robinson        King            Carter          Collins

这意味着,对于每个for,文件都会创建一个额外的实例,并且行会重复,这是我不希望发生的。我知道附近有工作吗?我有Python 2.4.3。谢谢

那是因为你写了两行

将循环更改为:

for line in fin:
    line = line.replace('Smith','Mr.Smith')
    line = line.replace('Walker','Mr.Walker')
    fout.write(line)

快速修复方法是应用链式
str.replace
调用:

for line in fin:
   fout.write(line.replace('Smith', 'Mr.Smith').replace('Walker','Mr.Walker'))

但请注意,
str.replace
的一个问题是,这也会影响类似于
Smithy
的字符串,要处理这种情况,请使用带单词边界的正则表达式,或者先将文件中的每一行转换为列表,然后更改列表中的项目。

如果我想更改整行,该怎么办?比如说让布朗·哈里斯·沃克·格林·菲利普斯变成布朗先生、哈里斯先生、沃克·格林·菲利普斯先生?那么用正则表达式替换可能会更好。尝试将
import re
添加到顶部,并使用:
line=re.sub(r'(\w+),r'Mr.\1',line)
进行循环内的替换。如果我想更改整行呢?像让布朗哈里斯沃克格林菲利普斯先生布朗哈里斯沃克格林菲利普斯先生?埃利亚斯的想法似乎对我的申请更有帮助。谢谢你的帮助!否
Green
Phillips
前面的
Mr.
?否。某些内容要求相同。这就像是我的代码应该是什么的一个例子。我的实际文件有很多东西是永远不会改变的!