我正在嘗試編寫一個用於從保存在文件中的存檔列表中寫入sqlite表的小腳本。到目前爲止的代碼是這樣的:python x64中的編碼問題
import os import _sqlite3 import sys
print sys.path[0] mydir = sys.path[0] print (mydir) def listdir(mydir):
lis=[]
for root, dirs, files in os.walk(mydir):
for name in files:
lis.append(os.path.join(root,name))
return lis
filename = "list.txt" print ("writting in %s" % filename) file = open(filename, 'w') for i in listdir(mydir):
file.write(i)
file.write("\n") file.close()
con =
_sqlite3.connect("%s/conection"%mydir) c=con.cursor()
c.execute(''' drop table files ''') c.execute('create table files (name text, other text)') file = open(filename,'r') for line in file :
a = 1
for t in [("%s"%line, "%i"%a)]:
c.execute('insert into files values(?,?)',t)
a=a+1 c.execute('select * from files') print c.fetchall() con.commit() c.close()
當我運行得到如下:
Traceback (most recent call last): File "C:\Users\josh\FORGE.py", line 32, in <module>
c.execute('insert into files values(?,?)',t) ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
從來就試圖與Unicode的()內置函數,但仍不需額外的工作,他說,他不能解碼字符0xed或什麼的。
我知道問題出在列表字符串的編碼上,但是我找不到一種方法將它們放在正確的位置。有任何想法嗎?提前致謝!
http://farmdev.com/talks/unicode/ – 2010-11-11 20:17:37