编码日语字符时出现字符映射错误
charmap error when encoding Japanese characters
我正在编写一个程序,使用 replace()
函数将特定日文字符从外部文本文件翻译成英文拼写,但我遇到了一个奇怪的错误。
我确保对文本文件中的所有字符进行编码,然后将其放入一个变量,然后在该变量上以字节级别开始替换过程,然后再次将其解码为字符串,然后写入到一个新的文本文件。
path = input('Location: ').strip('"')
txt = ''
with open(path,'rb') as f:
txt = f.read()
def convert(jchar,echar):
ct = txt.replace(jchar.encode('utf-8'),echar.encode('utf-8'))
return ct
txt = convert('ぁ','a')
txt = convert('っ','su')
with open('Translated.txt','w') as tf:
tf.write(txt.decode('utf-8'))
input('Done.')
如果文本文件包含所有可在脚本中替换的日文字符,一切都会完美,但如果文本文件包含在脚本中不可替换的日文字符,我会收到此错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\u306e' in position 6: character maps to <undefined>
据此,python 似乎无法在编码后再次解码日文字符的字节。
最糟糕的是,甚至还有一些其他非 Unicode 字符,即使我在 python 脚本上将其替换,我仍然会得到同样的错误,这意味着 python甚至无法对其进行编码,但我现在的主要关注点是为什么 python 拒绝解码日文字符的字节,尽管 python 它自己能够对其进行编码。
您需要在打开要写入的文件时设置正确的编码,例如:
with open('Translated.txt','w', encoding='utf-8') as tf:
Python defaults to a specific encoding 取决于您 运行 使用的平台。在 Windows 上,它可能是 ASCII。当您尝试将字符写入文件时,它会尝试将字节解码为 ASCII(或系统默认的任何非 Unicode 编码)字符串,但该字节没有 ASCII 字符,因此失败.
当您替换字符时它起作用的原因是罗马字符可以写成 ASCII,并且因为当您尝试写入文件时会发生错误。如果您查看打印的 Traceback,您会确切地看到它发生的位置:
Traceback (most recent call last):
File ".\sandbox.py", line 61, in <module>
tf.write(txt.decode('utf-8'))
File "[...]\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u3041' in position 11: character maps to <undefined>
我找到了一个修复程序,但我不知道它为什么起作用,我从最后一行中删除了 .decode('utf-8') 并且修复了整个问题,即使是我提到的最糟糕的问题,我认为使用 as 方法自动解码为 bytes
我正在编写一个程序,使用 replace()
函数将特定日文字符从外部文本文件翻译成英文拼写,但我遇到了一个奇怪的错误。
我确保对文本文件中的所有字符进行编码,然后将其放入一个变量,然后在该变量上以字节级别开始替换过程,然后再次将其解码为字符串,然后写入到一个新的文本文件。
path = input('Location: ').strip('"')
txt = ''
with open(path,'rb') as f:
txt = f.read()
def convert(jchar,echar):
ct = txt.replace(jchar.encode('utf-8'),echar.encode('utf-8'))
return ct
txt = convert('ぁ','a')
txt = convert('っ','su')
with open('Translated.txt','w') as tf:
tf.write(txt.decode('utf-8'))
input('Done.')
如果文本文件包含所有可在脚本中替换的日文字符,一切都会完美,但如果文本文件包含在脚本中不可替换的日文字符,我会收到此错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\u306e' in position 6: character maps to <undefined>
据此,python 似乎无法在编码后再次解码日文字符的字节。
最糟糕的是,甚至还有一些其他非 Unicode 字符,即使我在 python 脚本上将其替换,我仍然会得到同样的错误,这意味着 python甚至无法对其进行编码,但我现在的主要关注点是为什么 python 拒绝解码日文字符的字节,尽管 python 它自己能够对其进行编码。
您需要在打开要写入的文件时设置正确的编码,例如:
with open('Translated.txt','w', encoding='utf-8') as tf:
Python defaults to a specific encoding 取决于您 运行 使用的平台。在 Windows 上,它可能是 ASCII。当您尝试将字符写入文件时,它会尝试将字节解码为 ASCII(或系统默认的任何非 Unicode 编码)字符串,但该字节没有 ASCII 字符,因此失败.
当您替换字符时它起作用的原因是罗马字符可以写成 ASCII,并且因为当您尝试写入文件时会发生错误。如果您查看打印的 Traceback,您会确切地看到它发生的位置:
Traceback (most recent call last):
File ".\sandbox.py", line 61, in <module>
tf.write(txt.decode('utf-8'))
File "[...]\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u3041' in position 11: character maps to <undefined>
我找到了一个修复程序,但我不知道它为什么起作用,我从最后一行中删除了 .decode('utf-8') 并且修复了整个问题,即使是我提到的最糟糕的问题,我认为使用 as 方法自动解码为 bytes