Funny Chars that Python Hates

How to account for these..

Page 1 of 1

11 Replies - 7336 Views - Last Post: 17 July 2010 - 02:06 PM Rate Topic: -----

#1 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Funny Chars that Python Hates

Posted 27 May 2010 - 02:11 PM

Ñ
Õ
é





So Characters like these show up in my database some way some how. If I pull a record that contains one of these chars and then try and process it, Python blows up. I try doing u"%s" % variable and it still blows up. I have just decided to clean these things out, however, the e w/ the accent is very prominent for Decor of Home Decor considering I work w/ craft websites. I just can't believe Python doesn't have a way to handle this sort of thing. Could someone point me in the direction of some documentation?

You can't replace them because having the char in a python scripts makes things blow up. There has to be something out there that you can do to a string variable that will force Python to accept and process these types of chars. It is hard for me to find documentation cause I am not so keen on char coding. Don't really know what I am looking for, I know I have tried googling the errors these rise in the past, but I never really found an answer.

This post has been edited by supersssweety: 27 May 2010 - 02:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Funny Chars that Python Hates

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Funny Chars that Python Hates

Posted 27 May 2010 - 07:02 PM

I believe it involves character encoding, though I don't know for sure. Try specifying ascii or utf-8 and see if that helps.
Was This Post Helpful? 0
  • +
  • -

#3 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Funny Chars that Python Hates

Posted 27 May 2010 - 07:32 PM

Have you set the default encoding to a unicode set? My guess it's set to ascii (you can check by sys.getdefaultencoding()).


If this is the case, make a script called sitecustomize.py and put it in your PythonXX/Lib/site-packages folder if you don't already have it. Restart Python and check to see if it's different. The script should simply be:

import sys
sys.setdefaultencoding('iso-8859-1')




EDIT: I could be totally wrong because I have never encountered your problem :bananaman:

This post has been edited by SpeedisaVirus: 27 May 2010 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Funny Chars that Python Hates

Posted 27 May 2010 - 08:50 PM

Comment retracted. :sweatdrop:

This post has been edited by atraub: 28 May 2010 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Funny Chars that Python Hates

Posted 28 May 2010 - 01:08 PM

Add # -*- coding: utf-8 -*- as your second line of the file (under the shebang line)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

print """ 





"""


This runs and outputs correctly.
Was This Post Helpful? 1
  • +
  • -

#6 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Funny Chars that Python Hates

Posted 28 May 2010 - 08:30 PM

View Postatraub, on 28 May 2010 - 02:50 AM, said:

Comment retracted. :sweatdrop:


:'(

Programmable's suggestion was my next stop. I suck so much :stupid: then again I don't do much python
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Funny Chars that Python Hates

Posted 28 May 2010 - 11:49 PM

hehe well when you mentioned unicode, I just assumed my UTF-8 suggestion was wrong, so I had just made a comment to that effect :-P. Turns out it was UTF-8 for the win ^_^
Was This Post Helpful? 0
  • +
  • -

#8 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Funny Chars that Python Hates

Posted 01 June 2010 - 07:15 AM

...hrm I'll let you know how it works, there are lots of chars like this in the database and I am sending them through an API, I don't have the permission to press the button yet. The DBA is still having to work out some of the data on our end. Thanks for the reply though. I'll let you guys know how it goes.
Was This Post Helpful? 0
  • +
  • -

#9 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Funny Chars that Python Hates

Posted 01 June 2010 - 01:04 PM

View Postprogramble, on 28 May 2010 - 12:08 PM, said:

Add # -*- coding: utf-8 -*- as your second line of the file (under the shebang line)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

print """ 





"""


This runs and outputs correctly.


OK so that worked but I have run into another problems, see I pull my data, organize it, and then dump it into a variables like so:

title = product[1]


Where product[1] is from my database result set

Then I load it up for Suds like so:

array_of_inventory_item_submit = ca_client_inventory.factory.create('ArrayOfInventoryItemSubmit')
for product in products:
    inventory_item_submit = ca_client_inventory.factory.create('InventoryItemSubmit')
    inventory_item_list = get_item_list(product)
    inventory_item_submit = [inventory_item_list]
    array_of_inventory_item_submit.InventoryItemSubmit.append(inventory_item_submit)
#Call that service baby!
ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)


Where get_item_list sets product[1] to title and (including a whole bunch of other nodes):

inventory_item_submit.Title = title


So everything runs fine until I call ca_client_inventory.service.SynchInventoryItemList that contains array_of_inventory_item_submit which contains the title w/ the funky char...here is the error:

Traceback (most recent call last):
  File "upload_all_inventory_ebay.py", line 421, in <module>
    ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)
  File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 539, in __call__
  File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 592, in invoke
  File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 118, in get_message
  File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 63, in bodycontent
  File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 105, in mkparam
  File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 260, in mkparam
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 62, in process
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 198, in append
  File "build/bdist.macosx-10.6-i386/egg/suds/sax/element.py", line 251, in setText
  File "build/bdist.macosx-10.6-i386/egg/suds/sax/text.py", line 43, in __new__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 116: ordinal not in range(128)


Now what? My guess is my script can take in these funky chars because I have # -*- coding: utf-8 -*- at the top but Suds does NOT have that at the top of its files. Do I really want to go and change the Suds files...we all know this is the least desired last possible solution...what can I do?
Was This Post Helpful? -1
  • +
  • -

#10 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Funny Chars that Python Hates

Posted 02 June 2010 - 01:44 PM

Here is the answer via the Luper Rouch over there at stackoverflow:
title = product[1].decode("UTF-8")

works like a charm!
Was This Post Helpful? 0
  • +
  • -

#11 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Funny Chars that Python Hates

Posted 03 July 2010 - 12:15 PM

View Postsupersssweety, on 01 June 2010 - 02:04 PM, said:



Sorry supersssweety, I didn't mean to give you bad rep, the "was post helpful" box wasn't where it was supposed to be (stuck up and to the right and covering your post) and I clicked it by accident.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_yeerp*


Reputation:

Re: Funny Chars that Python Hates

Posted 17 July 2010 - 02:06 PM

switch to python 3?
Was This Post Helpful? 0

Page 1 of 1