[英]Decoding / Encoding Foreign Characters / UnicodeEncodeError: 'charmap' codec
I'm trying to export some tweets into json file but am getting errors. 我正在尝试将一些tweet导出到json文件中,但出现错误。 When I don't get errors it puts a 'b' character in my data and therefore messes up my json format. 当我没有收到错误时,它会在数据中添加一个'b'字符,因此会弄乱我的json格式。 Here's my example and code: 这是我的示例和代码:
here is the content of 'tweets': ['Мобильный телефон Xiaomi Redmi 6 64GB/4GB vs\\n Мобильный телефон Huawei Honor 8X 64GB\\n по параметрам совпадают на 86… ', 'Xiaomi Rilis Redmi Note 7 Pro di India, Ini Spesifikasinya'] 这是'tweets'的内容:['Мобильныйтелефон小米Redmi 6 64GB / 4GB vs \\ nМобильныйтелефонHuawei Honor 8X 64GB \\ nпопараметрамсовпадаютPro Pro Pro Pro Pro Pro Pro Pro Pro 86 Pro… “]
which I then convert to string: 然后我将其转换为字符串:
tweets2=str(tweets)
and when I try to export it: 当我尝试导出它时:
with open("tw_stream.jsonl", 'a') as f:
f.write(tweets2)
I get the following error: 我收到以下错误:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-
10: character maps to <undefined>
So the problem is I'm attempting to remove the 'b\\' part of the string as it's not valid json, but in doing so I cannot encode the foreign characters to "utf-8"... 所以问题是我试图删除字符串的'b \\'部分,因为它不是有效的json,但是这样做无法将外来字符编码为“ utf-8”。
If someone can help it would be much appreciated. 如果有人可以帮助,将不胜感激。
Thank you :-) 谢谢 :-)
您可以通过在其中传递param encoding =“ utf-8”来使用utf-8编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.