python - python文件写入显示UnicodeEncodeError的错误
问题描述
我需要制作 cp932(它已扩展shift-jis
)
UnicodeEncodeError: 'cp932' codec can't encode character '\u270c' 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()
我只是简化了mytext
这篇文章。
我认为这个字符通过忽略 flg 的编码。
但是,write
显示错误。
有没有办法解决这个问题??
解决方案
\
是 cp932 中的功能符号。所以,如果你想编码\
,你应该\\
在你的情况下使用:
import codecs
mytext = '\\u270c'
with codecs.open(path,mode='w',encoding='cp932') as f:
mytext.encode('cp932',"ignore")
f.write(mytext)
exit()
推荐阅读
- android - 迁移到 androidX 后,Retrofit 界面中的 Kapt NonExistentClass 异常
- javascript - fancybox 两个图像显示在同一个窗口
- python - 带有“+”选项的 argparse
- javascript - 用另一个 HTML 标记将 HTML 标记包装在纯字符串中
- javascript - React Native 未处理的异常未在 ErrorUtils 中捕获 & 发布 APK 显示空白屏幕而不是在 JS 异常上崩溃
- android - 在 Android Kotlin 中,assertNotNull 导致单元测试无法运行
- javascript - 使用不同的数据属性使多个计数器计数
- javascript - 如何在类方法中处理 redux 道具
- android - 无法在 Android Studio 模拟器中加载应用程序
- javascript - 如何在ngrx/effect(redux-observable)中调度多个动作?