我尝试用python做与下面java代码相同的事情。在
String decoded = new String("ä¸".getBytes("ISO8859_1"), "UTF-8");
System.out.println(decoded);
输出是一个中文字符串“cip”。在
在Python中,我尝试了encode/decode/bytearray,但总是得到无法读取的字符串。我想我的问题是我不太了解java/python编码机制是如何工作的。我也无法从现有的答案中找到解决办法。在
^{pr2}$ä¸- -- <type 'str'>
ä¸Â- -- <type 'str'>
ä¸Â- -- <type 'bytearray'>
ä¸Â- -- <type 'str'>
ä¸Â- -- <type 'str'>
ch = 'masanori harigae ã\201®ã\203\221ã\203¼ã\202½ã\203\212ã\203«ä¼\232è-°å®¤'
print ch.decode('utf-8').encode('iso-8859-1')
我得到了
回溯(最近一次呼叫): 文件“”,第1行,输入 文件“/apps/Python/lib/python2.7/encodings/utf_8.py”,第16行,在decode中 返回codecs.utf_8_解码(输入,错误,真) UnicodeDecodeError:“utf8”编解码器无法解码位置19中的字节0x81:起始字节无效
Java代码:
String decoded = new String("masanori harigae ã\201®ã\203\221ã\203¼ã\202½ã\203\212ã\203«ä¼\232è-°å®¤".getBytes("ISO8859_1"), "UTF-8");
System.out.println(decoded);
输出是masanori harigaeのパーソナルょ室
你这样做是不对的。有一个bytestring被错误地编码为utf-8,您希望它被解释为iso-8859-1:
相关问题 更多 >
编程相关推荐