sunrise

每天不断学习,才能不断提升自己。

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 0 文章 :: 92 评论 :: 0 Trackbacks
本来是为了省事处理数据的时候批量插入,结果插入的时候一直不能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,查了好久。
posted on 2013-02-21 16:39 SunRise_at 阅读(9654) 评论(2)  编辑 收藏 引用 所属分类: python解决方案

评论

# re: UnicodeDecodeError: 'utf8' codec can't decode bytes in position 18-19: unexpected end of data 2013-10-16 11:01 opina
那这个问题是怎么解决的呢?  回复  更多评论
  

# re: UnicodeDecodeError: 'utf8' codec can't decode bytes in position 18-19: unexpected end of data 2013-10-16 11:02 opina
Traceback (most recent call last):
File "C:/Python25/testPaython/terry3.py", line 35, in <module>
sheet1.write(row,1,customerid.decode('utf8'))
File "C:\Python25\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 2-3: invalid data

这是我报的错
sheet1.write(row,1,customerid.decode('utf8'))
这个是报错行,求解  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理