2013-12-16 45 views
0

對不起我的英語不好。我有一些代碼:Pyodbc,轉換爲字符串的值

import csv, pyodbc 
MDB = 'base.mdb' 
DRV = '{Microsoft Access Driver (*.mdb)}' 
PWD = 'pw' 
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV, MDB, PWD)) 
cur = con.cursor() 
SQL = 'SELECT * FROM Units;' # your query goes here 
rows = cur.execute(SQL).fetchall() 
for row in rows: 
    for r in row: 
     print str(r) 
cur.close() 
con.close() 

當我執行此我有回溯:

print str(r) 

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not inrange(128)

我怎麼可以這樣symbolst轉換爲字符串。謝謝你的幫助。

+0

http://docs.python.org/2/library/stdtypes.html#str.decode檢查那裏和http://docs.python.org/2/library/stdtypes.html#str.encode –

+0

print str(r).encode(「utf-8」) UnicodeEncodeError:'ascii'編解碼器不能編碼0-1位字符:序號不在範圍內(128) –

+0

print str(r).encode(「utf- 8「) UnicodeEncodeError:'ascii'編解碼器無法編碼位置0-1中的字符:序號不在範圍內(128) –

回答

0
r.encode('utf-8') 

或可能

unicode(r) 

The unicode() constructor has the signature unicode(string[, encoding, errors]). All of its arguments should be 8-bit strings. The first argument is converted to Unicode using the specified encoding; if you leave off the encoding argument, the ASCII encoding is used for the conversion, so characters greater than 127 will be treated as errors:

編輯:所以這個問題是要輸出到不支持這些字符(UnicodeEncodeError: 'charmap' codec can't encode - character maps to <undefined>, print function)和你最好的選擇可能是替換字符終端在可接受的範圍之外。

如果你刪除了打印語句,或者乾脆繼續操作數據,事情就會順利進行(python可以在內部處理字符串)。

如果您擔心的東西看起來不正確嘗試將您的內容推送到使用正確編碼(UTF-8或UTF-16應處理俄語字符)編碼的文件。

+0

我可以用俄語字母做什麼? –