473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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(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
1 6506
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

2
by: Thomas | last post by:
I was used to pass Unicode strings to Tk widgets. IIRC, Tcl/Tk expects UTF-8 encoded strings, but Tkinter took care of that. This worked, as long as I was using Python/Tk on Red Hat 9 Linux (and...
4
by: gabor | last post by:
hi, today i made some tests... i tested some unicode symbols, that are above the 16bit limit (gothic:http://www.unicode.org/charts/PDF/U10330.pdf) .. i played around with iconv and so on,...
4
by: Ricardo Bugalho | last post by:
Hello, I'm using Python 2.3.4 and I noticed that, when stdout is a terminal, the 'print' statement converts Unicode strings into the encoding defined by the locales instead of the one returned by...
2
by: Miki Tebeka | last post by:
Hello All, I'm shipping an application using py2exe. With Python2.3 it worked fine but when switching to Python2.4 I started getting "warning: string/unicode conversion" all over the place. ...
5
by: Xah Lee | last post by:
python has this nice unicodedata module that deals with unicode nicely. #-*- coding: utf-8 -*- # python from unicodedata import * # each unicode char has a unique name. # one can use the...
6
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2...
9
by: thijs.braem | last post by:
Hi everyone, I'm having quite some troubles trying to convert Unicode to String (for use in psycopg, which apparently doesn't know how to cope with unicode strings). The error I keep having...
20
by: weheh | last post by:
Dear web gods: After much, much, much struggle with unicode, many an hour reading all the examples online, coding them, testing them, ripping them apart and putting them back together, I am...
0
by: santhescript01 | last post by:
Unicode to non unicode conversion problem -------------------------------------------------------------------------------- Hi All, I am using C dll in macro which converts Unicode data to...
1
by: Eric S. Johansson | last post by:
I'm having a problem (Python 2.4) converting strings with random 8-bit characters into an escape form which is 7-bit clean for storage in a database. Here's an example: body =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.