Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;UnicodeDecodeError:&x27;ascii';编解码器可以';t解码字节";使用ElementTree处理中文命令行参数时_Python_Utf 8_Encode_Elementtree_Python 2.x - Fatal编程技术网

Python &引用;UnicodeDecodeError:&x27;ascii';编解码器可以';t解码字节";使用ElementTree处理中文命令行参数时

Python &引用;UnicodeDecodeError:&x27;ascii';编解码器可以';t解码字节";使用ElementTree处理中文命令行参数时,python,utf-8,encode,elementtree,python-2.x,Python,Utf 8,Encode,Elementtree,Python 2.x,我有一个XML文件test.XML,内容如下: <?xml version='1.0' encoding='UTF-8'?> <Configurations version="1.0"> <Item name="a" value="avalue" /> <Item name="b" value="bvalue" /> </Configurations> 当我像这样执行python文件时:“encode.py测试 bva

我有一个XML文件
test.XML
,内容如下:

<?xml version='1.0' encoding='UTF-8'?>
<Configurations version="1.0">
    <Item name="a" value="avalue" />
    <Item name="b" value="bvalue" />
</Configurations>
当我像这样执行python文件时:“encode.py测试 bvalue”在Windows命令提示符中,以异常结束:

D:\Test_study\python\encode>encode.py 测试 bvalue
Traceback (most recent call last):
File "D:\Test_study\python\encode\encode.py", line 25, in <module>
    Modify_Config()
File "D:\Test_study\python\encode\encode.py", line 20, in Modify_Config
    tree.write('./test.xml', encoding="UTF-8")
File "C:\Python27\lib\xml\etree\ElementTree.py", line 821, in write
    serialize(write, self._root, encoding, qnames, namespaces)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 940, in _serialize_xml
   _serialize_xml(write, e, encoding, qnames, None)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 933, in _serialize_xml
   v = _escape_attrib(v, encoding)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1091, in _escape_attrib
   return text.encode(encoding, "xmlcharrefreplace")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position 0: ordinal
not in range(128)
D:\Test\u study\python\encode>encode.py测试 B值
回溯(最近一次呼叫最后一次):
文件“D:\Test\u study\python\encode\encode.py”,第25行,在
修改_Config()
文件“D:\Test\u study\python\encode\encode.py”,第20行,在Modify\u Config中
tree.write('./test.xml',encoding=“UTF-8”)
写入文件“C:\Python27\lib\xml\etree\ElementTree.py”,第821行
序列化(写入、自根、编码、qnames、命名空间)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第940行,在xml中序列化
_序列化xml(写入、e、编码、qnames、无)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第933行,在_serialize_xml中
v=_escape_attrib(v,编码)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第1091行,在_escape\u attrib中
返回text.encode(编码,“xmlcharrefreplace”)
UnicodeDecodeError:“ascii”编解码器无法解码位置0:ordinal中的字节0xb2
不在范围内(128)

为什么会出现这种错误?我的操作系统是windows 7,64位字节字符串和unicode字符串只有在字符数小于128时才可交换。元素sys.argv内容是python 2.x中的字节字符串。此外,它们似乎是用GB2312编码的,而不是UTF-8。因此,您需要明确:

a_value = sys.argv[1].decode('GB2312')
b_value = sys.argv[2].decode('GB2312')

谢谢。argv的编码是GB2312。但是代码“a_value=sys.argv[1]。decode('GB2312')”在我的Windows命令提示符中有错误,错误是“回溯(最近一次调用):文件“D:\Test\u study\python\encode\encode.py”,第22行,在a_value=sys.argv[1]中。decode('UTF-8')文件“C:\Python27\lib\encodings\UTF\u 8.py”“,第16行,在解码返回编解码器中。utf_8_解码(输入,错误,真)UnicodeDecodeError:“utf8”编解码器无法解码位置3:无效的起始字节中的字节0xb0”,对于复杂的错误信息,抱歉。
a_value = sys.argv[1].decode('GB2312')
b_value = sys.argv[2].decode('GB2312')