简体   繁体   English

python文件写入显示UnicodeEncodeError的错误

[英]python file writing shows the error for UnicodeEncodeError

I need to make the cp932 (it is expanded shift-jis )我需要制作 cp932 (它是扩展的shift-jis

UnicodeEncodeError: 'cp932' codec can't encode character '\✌' in position 0: illegal multibyte sequence

    import codecs
    mytext = '\u270c'
    with codecs.open(path,mode='w',encoding='cp932') as f:
        mytext.encode('cp932',"ignore")
        f.write(mytext)
    exit()

I just simplify the mytext for this article.我只是简化了这篇文章的mytext

I think this character pass the encode with ignore flg.我认为这个字符通过了忽略标志的编码。

However, write shows the error.但是, write显示错误。

Is there any way to solve this??有没有办法解决这个问题??

\\ is functional symbol in cp932. \\是 cp932 中的功能符号。 So, If you want to encode \\ you should use the \\\\所以,如果你想对\\进行编码,你应该使用\\\\
in your case :在你的情况下:

import codecs
mytext = '\\u270c'
with codecs.open(path,mode='w',encoding='cp932') as f:
    mytext.encode('cp932',"ignore")
    f.write(mytext)
exit()

In your example, the file f will expect Unicode strings to be passed to f.write() and they will be encoded as declared by codecs.open , so the code is trying to double-encode.在您的示例中,文件f将期望将 Unicode 字符串传递给f.write()并且它们将按照codecs.open声明进行编码,因此代码正在尝试进行双重编码。 Also, '\✌' is not a character supported by CP932, so it can't be written to a CP932 file in any case.此外, '\✌'不是 CP932 支持的字符,因此在任何情况下都不能写入 CP932 文件。

Assuming Python 3, to write a Unicode string text in a particular encoding, use:假设 Python 3,要以特定编码编写 Unicode 字符串text ,请使用:

with open('output.txt','w',encoding='cp932') as f:
    f.write(text)

codecs is an older module and isn't needed. codecs是一个较旧的模块,不需要。 In Python 2, io.open is the backported equivalent to Python 3's open and is also supported by Python 3, for portability.在 Python 2 中, io.open是与 Python 3 的open等效的向后移植,并且 Python 3 也支持可移植性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM