By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

Python unicode conversion to UTF-8

P: 1
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(encoding, [,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!
Apr 14 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Try one of these:
Expand|Select|Wrap|Line Numbers
  1. >>> str(u'xyz')
  2. 'xyz'
  3. >>> u'xyz'
  4. u'xyz'
  5. >>> s = u'xyz'
  6. >>> s.encode('utf-8')
  7. 'xyz'
Apr 14 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.