954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Expected character buffer object Error

Hi,

I'm trying to replace an item in a list I made with 0.

popdata[0][5].replace(0.02,0)

when I run this it gives me the error TypeError: expected a character buffer object. I was wondering what this error meant and how I would go about fixing it. Thanks!

Elise

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Do before replace

print popdata[0][5], type(popdata[0][5])

To check your assumptions about it being list of floats.

You should have got this error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    [0.02,10.05,34,56,0.02,0.199999].replace(0.02,0)
AttributeError: 'list' object has no attribute 'replace'


How to replace float near some value:

>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if x==0.2 else x for x in numberlist ]
>>> print new_numberlist
[0.02, 10.050000000000001, 34, 56, 0.02, 0.19999900000000001]
>>> new_numberlist = [ 0 if abs(x-0.02)<1e-6 else x for x in numberlist ]
>>> print new_numberlist
[0, 10.050000000000001, 34, 56, 0, 0.19999900000000001]
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

EDIT:
To replace float with other number in list:

>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if x==0.02 else x for x in numberlist ]
>>> print(new_numberlist)
[0, 10.05, 34, 56, 0, 0.199999]
>>> ## more safe
>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if abs(x-0.02)<1e-6 else x for x in numberlist ]
>>> print(new_numberlist)
[0, 10.05, 34, 56, 0, 0.199999]
>>>
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I repro-d your error with this:

"".replace(0.02,0)


What exactly are you trying to do?

jcao219
Posting Pro in Training
417 posts since Dec 2009
Reputation Points: 28
Solved Threads: 97
 

Oh, my problem is that all my numbers where strings and not floats! I think the code above will work for me once I convert the numbers to floats. Thanks!

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Hmm I can't seem to convert my strings to floats. I wrote a for loop to do it that looks like this

for i in range(int(P)):
    for j in range(int(A)):
        float([popdata[i][j]])

where P and A are the indicies of my array (its not really a list I guess, it's an array. I don't know if that changes things). When I do this I get an error that says the float argument must be a string or a number. Is there anything I should be doing differently?

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Try this. You had unneeded brackets.

for i in range(int(P)):
    for j in range(int(A)):
        float(popdata[i][j])
Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

If the characters are floats they won't be in these '' in the array, correct? because mine still are. I'm not sure why.

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

You are not saving them, I think you are aware of it, but maybe not, so...

for i in range(int(P)):
    for j in range(int(A)):
        popdata[i][j] = float(popdata[i][j])


Like this youre list stays altered in the end.

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

I was definitely not aware of that. Thanks. This the third program I have written ever and I am still getting used to things like that. Ok, so now all of my numbers are floats. However my replacement code is still not working. What I have right now is

popdata2=[0 if x<0.05 else x for x in popdata1]
print(popdata2)

where popdata1 is my array of floats. Would you have any idea why it is not replacing things less than 0.05 with a 0. thanks!

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Here it is.

popdata2 = [x if x > 0.05 else 0 for x in popdata1]
print(popdata2)
Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

If you have exact limits I would have recommended staying with strings as much as possible as converting to float loose little accuracy so happens like this:

>>> a=0.33
>>> a
0.33000000000000002

See my second, safer alternative with abs() comparison for nearly same floats.

Unfortunately, as you need to do comparison with the different size numbers, maybe you need to change the numbers to floats. No came to my mind you can filter them like this:

numberlist = ['0.02','10.05','34','56','0.02','0.199999']
numberlist = [ '0' if float(x)<0.21 else x for x in numberlist]
print(numberlist)


Output:

['0', '10.05', '34', '56', '0', '0']
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I'm still not able to use this code. I am wondering exactly what x is and if maybe my program doesn't know what x is, and that is why I am having the problem. When I do float(x) it tells me that needs to be a string or a number and all of the items in popdata are strings. Could it be that when I say x it thinks the entire list that is popdata[0] etc.?

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Tell us what popdata is.
Is it a list of lists?

If so, you'd need something like this:

popdata = [['0' if float(x)<0.21 else x for x in l] for l in popdata]
jcao219
Posting Pro in Training
417 posts since Dec 2009
Reputation Points: 28
Solved Threads: 97
 

Could be enlightening to get my pretty printer function: http://www.daniweb.com/code/snippet281208.html

and do:

import pretty
pretty.ppr(popdata)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

yes popdata is a list of lists.

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
yes popdata is a list of lists.

So which list(s) from popdata do you want to change?

jcao219
Posting Pro in Training
417 posts since Dec 2009
Reputation Points: 28
Solved Threads: 97
 

Ahh yes! that was the problem. I was telling it to check if a list was less than 0.05. Thanks so much for all of your help!

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: