编码UTF-16更改代码的输出

发布于 2025-02-10 19:20:16 字数 523 浏览 1 评论 0原文

当我尝试使用编码数据的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2025-02-17 19:20:16

您的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文