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: 1title_english: A Title in Englishtitle_russian: "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u043D\u0430\\ \u0440\u0443\u0441\u0441\u043A\u043E\u043C"
- id: 2title_english: Another Titletitle_russian: "\u0414\u0440\u0443\u0433\u043E\u0439 \u041D\u0430\u0437\u0432\u0430\\u043D\u0438\u0435"
desired YAML output
- id: 1title_english: A Title in Englishtitle_russian: Название на русском
- id: 2title_english: Another Titletitle_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 headersfor 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!