Python String Replace Error

被刻印的时光 ゝ 提交于 2021-02-18 11:52:38

问题


I have a python script that keeps returning the following error:

TypeError: replace() takes at least 2 arguments (1 given)

I cannot for the life of me figure out what is causing this.

Here is part of my code:

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()

Everything that is seen within double quotations needs to be replaced with nothing.

So set([u' should look like


回答1:


This is what you want to do:

for line in inHandler:
    line = line.replace("set([u'", "")
    line = line.replace("'", "")
    line = line.replace("u'", "")
    line = line.replace("'])", "")

outHandler.write(line)

On the documentation, wherever it says something like str.replace(old,new[,count]) the str is an example variable. In fact, str is an inbuilt function, meaning you never want to change what it means by assigning it to anything.

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.



回答2:


There are two ways to call replace.

Let us start by defining a string:

In [19]: s = "set([u'"

We can call the replace method of string s:

In [20]: s.replace("u'", "")
Out[20]: 'set(['

Or, we can call the replace of the class str:

In [21]: str.replace(s, "u'", "")
Out[21]: 'set(['

The latter way requires three arguments because str. That is why you received the error about missing arguments.

What went wrong

Consider the code:

for line in inHandler:

    str = str.replace("set([u'", "")
    str = str.replace("'", "")
    str = str.replace("u'", "")
    str = str.replace("'])", "")

First, note the goal is to replace text in line but nowhere in the calls to replace is the variable line used for anything.

The first call to replace generates the error:

>>> str.replace("set([u'", "")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: replace() takes at least 2 arguments (1 given)

Used in the above form, str.replace interprets its first argument as the string to replace. It is as if you wrote:

"set([u'".replace("")

In other words, it thinks that set([u' is the string to operate on and the replace function was given just one argument: the empty string. That it why the message is replace() takes at least 2 arguments (1 given).

What you need is to operate on the variable line:

line = line.replace("set([u'", "")

And so on for the remaining lines in the loop.




回答3:


I think this should work.

for s in inHandler:

    s = s.replace("set([u'", " ")  ## notice the space between the quotes
    s = s.replace("'", " ")
    s = s.replace("u'", " ")
    s = s.replace("'])", " ")

Please refrain from using built-in data types as variables (like you have used str).




回答4:


You could compress the four lines of replace code

str = str.replace("set([u'", "")
str = str.replace("'", "")
str = str.replace("u'", "")
str = str.replace("'])", "")

as,

str = re.sub(r"set\(\[u'|u'|'\]\)|'", r"", str)

Example:

>>> import re
>>> string = "foo set([u'bar'foobaru''])"
>>> string = re.sub(r"set\(\[u'|u'|'\]\)|'", r"", string)
>>> string
'foo barfoobar'



回答5:


I modified your code as below:

inHandler = open('test.txt', 'r')
outHandler = open('test1.txt', 'w')
data = ''
for line in inHandler.readlines():
    print 'src:' + line
    line = line.replace("set([u'", "")
    line = line.replace("u'", "")
    line = line.replace("'])", "")
    line = line.replace("'", "")
    data += line
    print 'replace:' + line
outHandler.write(data)

inHandler.close()
outHandler.close()

And I tested it. Result:

src:set([u'adg',u'dafasdf'])
replace:adg,dafasdf
src:set([u'adg',u'dafasdf'])
replace:adg,dafasdf
src:set([u'adg',u'dafasdf'])
replace:adg,dafasdf



回答6:


if you are referring to str.replace (string) inbuild function in python then

str.replace(old, new[, count])
    Return a copy of the string with all occurrences of substring old replaced by new. 
    If the optional argument count is given, only the first count occurrences are replaced.

Means you need to give 2 values.

And you are using line as variable so you can try.

local_string = ''
for line in inHandler:
    local_string = line
    local_string = local_string.replace("set([u'", "")
    local_string = local_string.replace("'", "")
    local_string = local_string.replace("u'", "")
    local_string = local_string.replace("'])", "")


来源:https://stackoverflow.com/questions/26901914/python-string-replace-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!