python处理windows文本报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4
来源:互联网 发布:域名出售页面 编辑:程序博客网 时间:2024/05/19 16:03
在windows下生成一份txt文档,在linux下用python做格式化处理,报错
Traceback (most recent call last): File "txt2xml.py", line 55, in <module> tree.write("1.xml") File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 820, in write serialize(write, self._root, encoding, qnames, namespaces) File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml _serialize_xml(write, e, encoding, qnames, None) File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 937, in _serialize_xml write(_escape_cdata(text, encoding)) File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1073, in _escape_cdata return text.encode(encoding, "xmlcharrefreplace")UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 42: ordinal not in range(128)
查资料说是编码不匹配,添加了编码设置。该代码块为文本格式转化,得到yolo训练集合的样式。
代码使用xml.etree.ElementTree ,具体代码如下:
[root@localhost tools]# vi txt2xml.py
#encoding=utf-8 from xml.etree import ElementTree as ET import sys #print sys.getdefaultencoding()#在这里重新设置编码reload(sys)sys.setdefaultencoding('utf-8')f = open("/data/1xiu/darknet/carData/car.list1")for line in f: line = line.decode("gbk").encode("utf-8").strip() lineArray = line.split(" ") root=ET.Element('annotation') folder = ET.SubElement(root, "folder") folder.text = "general_text" filename = ET.SubElement(root, "filename") filename.text=lineArray[0].decode("gbk").encode("utf8") size = ET.SubElement(root, "size") width = ET.SubElement(size, "width") width.text = lineArray[2] height = ET.SubElement(size,"height") height.text = lineArray[1] depth = ET.SubElement(size, "depth") depth.text = str(3) obj = ET.SubElement(root, "object") name = ET.SubElement(obj, "name") name.text = "plate" bnd = ET.SubElement(obj, "bndbox") xmin = ET.SubElement(bnd, "xmin") xmin.text = lineArray[3] ymin = ET.SubElement(bnd, "ymin") ymin.text = lineArray[4] xmax = ET.SubElement(bnd, "xmax") xmax.text = lineArray[3] + lineArray[5] ymax = ET.SubElement(bnd, "ymax") ymax.text = lineArray[4] + lineArray[6] tree=ET.ElementTree(root) #tree.write(lineArray[0].decode("gbk").encode("utf8")+".xml") tar = lineArray[0] + ".xml" tree.write("1.xml")
0 0
- python处理windows文本报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4
- Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法
- Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法
- UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe4 in position
- Python报错:UnicodeDecodeError: 'utf8' codec can't decode byte
- Python报错UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe0 in position 0: ordinal not in range
- python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 4: ordinal not in range
- python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 4: ordinal not in range
- 【python】pip安装报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 7: ordinal not i
- [报错] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 2
- pip 安装pandas报UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5错
- [python]解决Windows下安装第三方插件报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0:
- [python]解决Windows下安装第三方插件报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0:
- python UnicodeDecodeError: 'ascii' codec can't decode byte ...
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5
- Django UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
- python_使用tarfile压缩及解压的方法
- iOS开发之APP上架要填写什么?有什么要注意?
- 流媒体协议——DASH(chapter-6)
- 51nod 1096 距离之和最小(简单题)
- 情感分析相关网站博客
- python处理windows文本报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4
- odoo 路由继承
- javascript的图片上传
- Toast分析——实现自己的Toast,toast分析实现
- iOS 开发学习 正则表达式
- maven简单应用总结
- struts2接收参数
- SAP FI配置关键点
- Servlet中控制页面跳转方式