UnicodeEncodeError 'charmap' codec can't encode characters in position 1-12
ミャンマー語の文字列を Jinja2 テンプレートに貼り付けてテンプレートを保存しようとすると、このエラーが発生します。OSに必要なフォントをすべてインストールし、codec
libを使用しようとしました。psocess: python スクリプトは CSV ファイルをデータで解析し、ディクショナリを作成します。このディクショナリは、Jinja2 テンプレートで使用される変数に値を入力するために使用されます。ファイルへの書き込みの瞬間にエラーが発生します。Python 3.4 を使用しています。というパッケージがありますがpython-myanmar
、それは 2.7 用であり、自分のコードをダウングレードしたくありません。これをすべて読んでください:http://www.unicode.org/notes/tn11/、http://chimera.labs.oreilly.com/books/1230000000393/ch02.html#_discussion_31、https://code.google. com/p/python-myanmar/パッケージとインストールされたシステム フォント。文字列をエンコードできます.encode('utf-8')
、しかし.decode()
、エラーなしではできません! 問題は、どうすればコードをダウングレードせず、何か追加のものをインストールすることができますが、データをファイルに書き込む python 3.4 組み込み関数のみを使用するのが最善でしょうか?
C:\Users\...\autocrm.py in create_templates(csvfile_location, csv_delimiter, template_location, count
ies_to_update, push_onthefly, csv_gspreadsheet, **kwargs)
270 ### use different parsers for ventures due to possible difference in website design
271 ### checks if there is a link in CSV/TSV
--> 272 if variables['promo_link'] != '':
273 article_values = soup_the_newsletter_article(variables['promo_link'])
274 if variables['item1_link'] != '':
C:\Users\...\autocrm.py in push_to_ums(countries_to_update, html_template, **kwargs)
471 ### save to import.xml
472 with open(xml_path_upload, 'w') as writefile:
--> 473 writefile.write(template.render(**values))
474 print('saved the import.xml')
475
C:\Python34\lib\encodings\cp1252.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):
UnicodeEncodeError: 'charmap' codec can't encode characters in position 6761-6772: character maps to <undefined>
ところで、出力が UTF8 の場合、なぜ cp1251.py を指しているのですかsys.getdefaultencoding()
??
with open(template_location, 'r') as raw_html:
template = Template(raw_html.read())
print('writing to template: ' + variables['country_id'])
# import ipdb;ipdb.set_trace()
with open('rendered_templates_L\\NL_' +
variables['country_id'] + ".html", 'w', encoding='utf-8') as writefile:
rendered_template = template.render(**alldata)
writefile.write(rendered_template)