编码UTF-16更改代码的输出
当我尝试使用编码数据的UTF-8在CSV中输出数据时,将出现划界,但某些法语字符却没有出现。
如果我将编码类型更改为UTF-16,则法语字符看起来还不错,但是数据不再界定并显示在一行中。
我的代码:
def data_to_csv(data):
keys = data[0].keys()
with open('data.csv', 'w', newline='', encoding="utf-16") as output_file:
dict_writer = csv.DictWriter(output_file, keys, delimiter=",")
dict_writer.writeheader()
dict_writer.writerows(data)
预期输出: csv预期输出
使用utf-8:
When I try to output data in a CSV using UTF-8 encoding the data comes out delimited but some French characters do not appear.
If I change the encoding type to UTF-16 the French characters appear ok but the data is no longer delimited and appear in a single line.
My code:
def data_to_csv(data):
keys = data[0].keys()
with open('data.csv', 'w', newline='', encoding="utf-16") as output_file:
dict_writer = csv.DictWriter(output_file, keys, delimiter=",")
dict_writer.writeheader()
dict_writer.writerows(data)
Expected output:
CSV Expected Output
Actual output using UTF-8: CSV Actual Output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的UTF-8编码文件很好。只是您用来创建屏幕截图的任何程序都不支持UTF-8/不为其配置。
您的UTF-16编码文件也可能还不错。
Your UTF-8 encoded file is fine. Just whatever program you used to create that screenshot doesn't support UTF-8/isn't configured for it.
Your UTF-16 encoded file is likely fine as well.