这应该是微不足道的,但。。。!我正在给一个UTF-8编码的文件写信,文本包括“科特迪瓦”。据我所知,“ô”是U+00F4。字符在任何地方都能正确显示,但在文件中以U+C3B4结尾,该字符应位于Unicode块HANGUL_音节(“쎴").
任何用U+00F4替换U+C3B4的尝试似乎都不会改变任何东西——下面文件的所有四行都包含它
这就产生了一个问题,因为当文件最终写入数据库时,它会显示为“Côte d'Ivodia”
更新:如果我使用io.open(“Test.html”,“w”)作为f_out:在下面,那么文件包含正确的U+00F4,它显示为“?”,最终的数据库记录仍然显示为“Côte d'Ivoid”,尽管:-(
MWE:
from __future__ import unicode_literals
import io
line="The current population of Côte d'Ivoire is 26,051,291"
for c in line:
if ord(c) > 127:
print(c, c.encode('utf-8').hex())
line1 = line.replace(u"\uC3B4", "ô")
line2 = line.replace(c, u"\u00F4")
line3 = line.replace(c, "ô")
#with io.open("Test.html", "w", encoding="utf-8") as f_out:
with io.open("Test.html", "w") as f_out:
f_out.write(line+"\n")
f_out.write(line1+"\n")
f_out.write(line2+"\n")
f_out.write(line3+"\n")
十六进制编辑器:
00000000h: 54 68 65 20 63 75 72 72 65 6E 74 20 70 6F 70 75 ; The current popu
00000010h: 6C 61 74 69 6F 6E 20 6F 66 20 43 C3 B4 74 65 20 ; lation of Côte
00000020h: 64 27 49 76 6F 69 72 65 20 69 73 20 32 36 2C 30 ; d'Ivoire is 26,0
00000030h: 35 31 2C 32 39 31 0D 0A 54 68 65 20 63 75 72 72 ; 51,291..The curr
00000040h: 65 6E 74 20 70 6F 70 75 6C 61 74 69 6F 6E 20 6F ; ent population o
00000050h: 66 20 43 C3 B4 74 65 20 64 27 49 76 6F 69 72 65 ; f Côte d'Ivoire
00000060h: 20 69 73 20 32 36 2C 30 35 31 2C 32 39 31 0D 0A ; is 26,051,291..
00000070h: 54 68 65 20 63 75 72 72 65 6E 74 20 70 6F 70 75 ; The current popu
00000080h: 6C 61 74 69 6F 6E 20 6F 66 20 43 C3 B4 74 65 20 ; lation of Côte
00000090h: 64 27 49 76 6F 69 72 65 20 69 73 20 32 36 2C 30 ; d'Ivoire is 26,0
000000a0h: 35 31 2C 32 39 31 0D 0A 54 68 65 20 63 75 72 72 ; 51,291..The curr
000000b0h: 65 6E 74 20 70 6F 70 75 6C 61 74 69 6F 6E 20 6F ; ent population o
000000c0h: 66 20 43 C3 B4 74 65 20 64 27 49 76 6F 69 72 65 ; f Côte d'Ivoire
000000d0h: 20 69 73 20 32 36 2C 30 35 31 2C 32 39 31 0D 0A ; is 26,051,291..
右-您正在混合苹果和橙子,即Unicode代码点(以
U+XXXX
为符号)和字节(以\xXX
为符号的pythonical)如果你真的想写一个UTF-8文件,那么你基本上完成了!您正在编写UTF-8,其中
ô
恰好是一个编码为两个字节的字符相关问题 更多 >
编程相关推荐