我试图使用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)更多推荐
发布评论