[Tutor] str.replace error

Alan Gauld alan.gauld at yahoo.co.uk
Thu Apr 25 20:16:25 EDT 2019


On 25/04/2019 18:46, Roger Lea Scherer wrote:

> Here is my code:
> 
> import csv
> 
> with open('somefile') as csvDataFile:
>     csvReader = csv.reader(csvDataFile)
>     for row in range(100):

what happens if there are more rows in the file than 100?
Or even if there are less!?

>         a = "Basic P1"
>         str.replace(a, "")

where does str come from? You haven't assigned anything
to str, this is the first mention.

did you mean to do something like

for str in csvreader:

?


>         print(next(csvReader))
> 
> I get an error:
> 
> Traceback (most recent call last):
>   File "somefile", line 7, in <module>
>     str.replace(a, "")
> TypeError: replace() takes at least 2 arguments (1 given)

I would expect a name error. So I think the code you
sent is not the code that generated the error. But
without the original code we have no idea what str
actually is.

If it is some kind of string remember that Python strings
are immutable so to get the changed string you need
to store the result. Possibly something like:

str = str.replace(...)

> But I think I have 2 arguments: a being the "old" argument as per the
> documentation, "" being the "new" argument as per the documentation.
> 
> What am I missing?

We can't say because we can't see the code that produced the error.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list