Reputation: 255
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
Upvotes: 7
Views: 41930
Reputation: 14328
Conclusion: you have two issue:
str.replace
str
str.replace
for error TypeError: replace() takes at least 2 arguments (1 given)
the root cause is:
your code
str = str.replace("set([u'", "")
intension is to use str.replace
do replace work
the correct (one) syntax is:
newStr = oldStr.replace(fromStr, toStr[, replaceCount])
corresponding to your code, could be:
replacedLine = line.replace("set([u'", "")
Note: another syntax is:
newStr = str.replace(oldStr, fromStr, toStr[, replaceCount])
for full details please refer another post's answer
str
str
is Python reserved key word
Python 3
str
has many builtin functions
str
as normal variable
/function
nameso your code:
for line in inHandler:
str = str.replace("set([u'", "")
should change to (something like this):
for line in inHandler:
newLine = line.replace("set([u'", "")
or
for line in inHandler:
newLine = str.replace(line, "set([u'", "")
Upvotes: 1
Reputation: 21243
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("'])", "")
Upvotes: -1
Reputation: 251
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
Upvotes: 0
Reputation: 113834
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.
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.
Upvotes: 1
Reputation: 174706
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'
Upvotes: 0
Reputation: 13953
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
).
Upvotes: 0
Reputation:
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.
Upvotes: 7