Python unicode conversion to UTF-8

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chrysie
    New Member
    • Apr 2011
    • 1

    Python unicode conversion to UTF-8

    Hi,

    I am new to python. I am using python 2.6.6 with pyodbc-2.1.8 and pywin32-216 on Windows Vista.

    I was able to connect to MS Access with pyodbc and execute my SELECT statement to retrieve data from MS Access. However, what I have retrieved appeared to be in Unicode strings (e.g., u'xyz', etc.) which I could not use as keys to build a dictionary, and I could not use as strings to write regular expressions to match with certain patterns.

    I have tried using Unicode encoding like s.decode(encodi ng, [,error]) with default encoding (ASCII), and 'utf-8', and 'ignore' invalid characters for error flag, but could not get that to work.

    Are there any ways to convert the unicode string so that I can use it as key to build dictionary and use for regular expresssion matching? Some examples would be highly appreciated.

    Thank you in advance for your help!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try one of these:
    Code:
    >>> str(u'xyz')
    'xyz'
    >>> u'xyz'
    u'xyz'
    >>> s = u'xyz'
    >>> s.encode('utf-8')
    'xyz'

    Comment

    Working...