本來是為了省事處理數據的時候批量插入,結果插入的時候一直不能decode。
python代碼:
a=open(r"testdata.txt").read(20)
print list(a)
print '=========================================================================================='
print list(a.decode('utf8'))
print '=========================================================================================='

testdata數據:
0 1 test 這件衣服設計的正好看,正是我想要的 2013/2/23 zh
0 2 happys 這件衣服設計的正好看,正是我想要的 2013/11/21 zh 出現的錯誤:
['\xef', '\xbb', '\xbf', '0', '\t', '1', '\t', 't', 'e', 's', 't', '\t', '\xe8', '\xbf', '\x99', '\xe4', '\xbb', '\xb6', '\xe8', '\xa1']
==========================================================================================

Traceback (most recent call last):
File "E:/designerpage/test.py", line 4, in <module>
print list(a.decode('utf8'))
File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 18-19: unexpected end of data出現錯誤的原因是因為utf8格式一般是'\xef', '\xbb', '\xbf'三個一起出現,然后解析的,而在某處不能夠成三個一組的時候就會出現這樣的錯誤。如果將20改為18則可以正常執行。
當然如果是在python2.6里是可以運行的,python2.7就會出現如上錯誤。無比坑爹的bug,查了好久。