DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Converting to unicode (http://www.daniweb.com/forums/thread158189.html)

ihatehippies Nov 18th, 2008 11:34 pm
Converting to unicode
 
Does anyone know how to convert an instance to unicode?
I understand how to convert a string into unicode format

u=unicode(s, 'ASCII', ignore), however this assumes that s is already a string and converting s by the str(s) method will kick back an error if s contains unicode unfriendly.
The strings I'm processing are 'instances' and the process works fine until the instance contains a character outside of the normal 128 character range. I found this link that I think is closely related. http://www.python.org/dev/peps/pep-0349/ Does anyone have any experience with this?

ihatehippies Nov 19th, 2008 9:23 pm
Re: Converting to unicode
 
the following is a function within a 'worddocument' class. It basically parses tables in word documents.
    def GetTables(self):
        doc = self.app.Documents[0]
        tables = []
        result=[]
        for word_table in doc.Tables:
            rows = []
            for word_row in word_table.Rows:
                yield(word_row)#cannot convert to unicode
anyone know how to convert this to unicode. Its type is instance. Converting it to string will raise an error

ihatehippies Nov 20th, 2008 7:46 pm
Re: Converting to unicode
 
anyone know anything about encoding. The actual character that is throwing the error is a 1/2 symbol. Could I do
x='some string'
for each_letter in x:
        try:
              each_letter.encode(#what goes here?)
        except:
                try:
                    #some other encoding format??

ihatehippies Nov 21st, 2008 2:39 am
Re: Converting to unicode
 
as much fun as it is writing to myself.. can someone explain how I use:
"To convert a class instance to Unicode, a __unicode__ method can be defined by a class, analogous to __str__. "

Its found on this link http://www.python.org/doc/2.2.3/whatsnew/node8.html


All times are GMT -4. The time now is 10:53 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC