我試圖格式化文件,以便它可以插入到數據庫中,該文件最初是壓縮和大約1.3MB大。 每一行看起來是這樣的:parse.unquote_plus TypeError
398,%7EAnoniem+001%7E,543,480,7525010,1775,0
這是怎樣的代碼看起來像這樣解析這個文件:
Village = gzip.open(Root+'\\data'+'\\' +str(Newest_Date[0])+'\\' +str(Newest_Date[1])+'\\' +str(Newest_Date[2])\
+'\\'+str(Newest_Date[3])+' village.gz');
Village_Parsed = str
for line in Village:
Village_Parsed = Village_Parsed + urllib.parse.unquote_plus(line);
print(Village.readline());
當我運行程序我得到這個錯誤:
Village_Parsed = Village_Parsed + urllib.parse.unquote_plus(line);
file "C:\Python31\lib\urllib\parse.py", line 404, in unquote_plus string = string.replace('+', ' ') TypeError: expected an object with the buffer interface
任何想法這裏有什麼不對? 在此先感謝您的幫助:)
@JFSebastian:你真的嘗試過嗎?我得到和OP一樣的錯誤...除了他的初始化問題,你的代碼看起來在功能上等同於他的返回字節對象。 – 2009-11-04 11:11:08
@John Machin:我試過了(現在)。我找不到'unquote_plus_from_bytes',所以我們不得不求助於顯式的'bytes.decode'方法。 – jfs 2009-11-04 11:19:27
謝謝,您的解決方案效果很好,謝謝您指出我的其他錯誤(Machin和Sebestian)。 我不確定ascii是否是使用過的字符編碼,但據我所知,它沒有任何問題。 – user202459 2009-11-08 05:40:11