将CSV转换为YAML,使用Unicode?(Convert CSV to YAML, with Unicode?)

编程入门 行业动态 更新时间:2024-10-26 23:36:47
将CSV转换为YAML,使用Unicode?(Convert CSV to YAML, with Unicode?)

我试图使用Python 3.4将包含Unicode字符串的CSV文件转换为YAML文件。

目前,YAML解析器将我的Unicode文本转义为ASCII字符串。 我希望YAML解析器将Unicode字符串导出 Unicode字符串,而不使用转义字符。 当然,我在这里误解了一些东西,我会很感激任何帮助。

奖励要点 :Python 2.7如何做到这一点?

CSV输入

id, title_english, title_russian 1, A Title in English, Название на русском 2, Another Title, Другой Название

当前YAML输出

- id: 1 title_english: A Title in English title_russian: "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u043D\u0430\ \ \u0440\u0443\u0441\u0441\u043A\u043E\u043C" - id: 2 title_english: Another Title title_russian: "\u0414\u0440\u0443\u0433\u043E\u0439 \u041D\u0430\u0437\u0432\u0430\ \u043D\u0438\u0435"

所需的YAML输出

- id: 1 title_english: A Title in English title_russian: Название на русском - id: 2 title_english: Another Title title_russian: Другой Название

Python转换代码

import csv import yaml in_file = open('csv_file.csv', "r") out_file = open('yaml_file.yaml', "w") items = [] def convert_to_yaml(line, counter): item = { 'id': counter, 'title_english': line[0], 'title_russian': line[1] } items.append(item) try: reader = csv.reader(in_file) next(reader) # skip headers for counter, line in enumerate(reader): convert_to_yaml(line, counter) out_file.write( yaml.dump(items, default_flow_style=False) ) finally: in_file.close() out_file.close()

谢谢!

I'm trying to convert a CSV file, containing Unicode strings, to a YAML file using Python 3.4.

Currently, the YAML parser escapes my Unicode text into an ASCII string. I want the YAML parser to export the Unicode string as a Unicode string, without the escape characters. I'm misunderstanding something here, of course, and I'd appreciate any assistance.

Bonus points: how might this be done with Python 2.7?

CSV input

id, title_english, title_russian 1, A Title in English, Название на русском 2, Another Title, Другой Название

current YAML output

- id: 1 title_english: A Title in English title_russian: "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u043D\u0430\ \ \u0440\u0443\u0441\u0441\u043A\u043E\u043C" - id: 2 title_english: Another Title title_russian: "\u0414\u0440\u0443\u0433\u043E\u0439 \u041D\u0430\u0437\u0432\u0430\ \u043D\u0438\u0435"

desired YAML output

- id: 1 title_english: A Title in English title_russian: Название на русском - id: 2 title_english: Another Title title_russian: Другой Название

Python conversion code

import csv import yaml in_file = open('csv_file.csv', "r") out_file = open('yaml_file.yaml', "w") items = [] def convert_to_yaml(line, counter): item = { 'id': counter, 'title_english': line[0], 'title_russian': line[1] } items.append(item) try: reader = csv.reader(in_file) next(reader) # skip headers for counter, line in enumerate(reader): convert_to_yaml(line, counter) out_file.write( yaml.dump(items, default_flow_style=False) ) finally: in_file.close() out_file.close()

Thanks!

最满意答案

我遇到了同样的问题,这是我能够解决它的基础上面的例子

out_file.write(yaml.dump(items, default_flow_style=False,allow_unicode=True) )

包括allow_unicode=True解决问题。

也专门为python2使用safe_dump而不是dump来防止!!python/unicode与unicode文本一起显示。

out_file.write(yaml.safe_dump(items, default_row_style=False,allow_unicode=True)

I ran into the same issue and this was how I was able to resolve it based on your example above

out_file.write(yaml.dump(items, default_flow_style=False,allow_unicode=True) )

including allow_unicode=True fixes the issue.

also specifically for python2 make use of safe_dump instead of dump to prevent the !!python/unicode displaying along with the unicode text.

out_file.write(yaml.safe_dump(items, default_row_style=False,allow_unicode=True)

更多推荐

本文发布于:2023-08-03 15:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   CSV   YAML   Convert   Unicode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!