Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python String Replace Error

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

like image 885
ge0m3try Avatar asked Nov 13 '14 05:11

ge0m3try


1 Answers

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.
like image 75
AHuman Avatar answered Oct 04 '22 22:10

AHuman