我有一个 python 脚本,它不断返回以下错误:
类型错误:replace() 至少需要 2 个参数(给定 1 个)
我一生都无法弄清楚是什么原因造成的。
这是我的代码的一部分:
inHandler = open(inFile2, 'r')
outHandler = open(outFile2, 'w')
for line in inHandler:
str = str.replace("set([u'", "")
str = str.replace("'", "")
str = str.replace("u'", "")
str = str.replace("'])", "")
outHandler.write(str)
inHandler.close()
outHandler.close()
双引号内出现的所有内容都需要替换为空。
So set([u'
应该看起来像
这就是您想要做的:
for line in inHandler:
line = line.replace("set([u'", "")
line = line.replace("'", "")
line = line.replace("u'", "")
line = line.replace("'])", "")
outHandler.write(line)
在文档中,只要有类似的内容str.replace(old,new[,count])
the str
是一个示例变量。实际上,str
是一个内置函数,这意味着您永远不想通过将其分配给任何东西来改变它的含义。
line = line.replace("set([u'", "")
^This sets the string equal to the new, improved string.
line = line.replace("set([u'", "")
^ This is the string of what you want to change.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)