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

testdata數(shù)據(jù):
0 1 test 這件衣服設(shè)計(jì)的正好看,正是我想要的 2013/2/23 zh
0 2 happys 這件衣服設(shè)計(jì)的正好看,正是我想要的 2013/11/21 zh 出現(xiàn)的錯(cuò)誤:
['\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出現(xiàn)錯(cuò)誤的原因是因?yàn)閡tf8格式一般是'\xef', '\xbb', '\xbf'三個(gè)一起出現(xiàn),然后解析的,而在某處不能夠成三個(gè)一組的時(shí)候就會(huì)出現(xiàn)這樣的錯(cuò)誤。如果將20改為18則可以正常執(zhí)行。
當(dāng)然如果是在python2.6里是可以運(yùn)行的,python2.7就會(huì)出現(xiàn)如上錯(cuò)誤。無比坑爹的bug,查了好久。