Re: [Tutor] str.replace error

2019-04-26 Thread Mats Wichmann
On 4/25/19 10:29 PM, Steven D'Aprano wrote:
> On Thu, Apr 25, 2019 at 10:46:31AM -0700, Roger Lea Scherer wrote:
> 
>> with open('somefile') as csvDataFile:
>> csvReader = csv.reader(csvDataFile)
>> for row in range(100):
>> a = "Basic P1"
>> str.replace(a, "")
>> print(next(csvReader))
> 
> 
> I'm not quite sure what you expected this to do, but you've 
> (deliberately? accidently?) run into one of the slightly advanced 
> corners of Python: unbound methods. 

accidentally, I believe.

notice that the way the Python 3 page on string methods is written, you
_could_ read it as you are to use the literal 'str' but in fact you are
expected to substitute in the name of your string object.

For this specific case:
===
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.
===

So for the example above you're expected to do (without changing the
range call, which has been commented on elsewhere: you should just
iterate directly over the reader object, that's the way it's designed):

for row in range(100):
a = "Basic P1"
row.replace(a, "")

and then hopefully actually do something with the modified 'row', not
just go on to the next iteration and throw it away...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] str.replace error

2019-04-26 Thread Alan Gauld via Tutor
On 26/04/2019 05:29, Steven D'Aprano wrote:

> (deliberately? accidently?) run into one of the slightly advanced 
> corners of Python: unbound methods.

Ooh, good catch. I completely forgot that the string class'
name is str...

That's why he didn't get a name 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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] str.replace error

2019-04-25 Thread Steven D'Aprano
On Thu, Apr 25, 2019 at 10:46:31AM -0700, Roger Lea Scherer wrote:

> with open('somefile') as csvDataFile:
> csvReader = csv.reader(csvDataFile)
> for row in range(100):
> a = "Basic P1"
> str.replace(a, "")
> print(next(csvReader))


I'm not quite sure what you expected this to do, but you've 
(deliberately? accidently?) run into one of the slightly advanced 
corners of Python: unbound methods. Apologies in advance, but this may 
be a bit technical. For a suggestion on how to fix the problem, see 
right at the end.

Normally when you call a string method, you do something like this:

mystring = "Something here"
newstring = mystring.replace("here", "there")

The technical name for the mystring.replace call is a "bound method", as 
the method is "bound" to a string instance (mystring). The method 
actually takes *three* arguments: the string to operate on, the 
substring to be replaced, and the replacement string:

   (string to operate on) . replace ( substring to be replaced, replacement )

But under the hood, what that turns into is an "unbound method" that 
calls the method attached to the *class* "str":

str.replace(string to operate on, substring to replace, replacement)

Alas, when you call an *unbound* method like this:

str.replace(a, "")

the interpreter may get a bit confused and report a slightly inaccurate 
error message:

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

For technical reasons, all the counts are off by one. The error message 
should say:

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

but because the same error message gets used for both bound and unbound 
methods, the counts are off for unbound methods. This is an annoyance, 
but given that unbound methods are relatively rare, not a large one.

So how do you fix your code?

(1) First of all, you need a string to operate on. I'm not sure which 
string that should be -- do you want to check *every* cell? Just the 
first cell in each row? Every second row? That's up to you to decide.

(2) Secondly, strings in Python are "immutable" -- that is to say, they 
cannot be changed in place. Unlike languages like (say) Pascal, if you 
want to change the value of a string, you need to re-assign a new 
string:

a = "Hello World!"
a.upper()  # Doesn't work!!!
print(a)  # Still gives "Hello World!"

Instead you need to say a = a.upper().

(3) Unbound methods are a slightly advanced technique which you don't 
need here, so you should just use normal method call.



Putting those three changes together gives something like this:


with open('somefile') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in range(100):  # How do you know there are only 100 rows?
mystring = "Something here? ~Basic P1~ but I don't know what"
print("Before:", mystring)
mystring = mystring.replace("Basic P1", "")
print("After:", mystring)
print(next(csvReader))



Hopefully that's enough to set you on the right track. If not, please 
feel free to write back to the list and ask more questions!




-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] str.replace error

2019-04-25 Thread Alan Gauld via Tutor
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 
> 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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] str.replace error

2019-04-25 Thread Roger Lea Scherer
I'm working wtih python 3.7 on Windows 10.
I'm trying to write some code in order to clean up the data in the csv file.
Using spreadsheet language, I want to replace part of a cell ("Basic P1")
with an empty string and write it in the comments cell.
I thought assigning a variable and replacing the string would be a good
idea.

Here is my code:

import csv

with open('somefile') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in range(100):
a = "Basic P1"
str.replace(a, "")
print(next(csvReader))

I get an error:

Traceback (most recent call last):
  File "somefile", line 7, in 
str.replace(a, "")
TypeError: replace() takes at least 2 arguments (1 given)

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?

-- 
Roger Lea Scherer
623.255.7719

  *Strengths:*
   Input, Strategic,
Responsibility,

Learner, Ideation
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor