1
UnicodeEncodeError 'charmap' codec can't encode characters in position 1-12

我在尝试将缅甸语字符串粘贴到 Jinja2 模板并保存模板时收到此错误。我在操作系统中安装了所有需要的字体,尝试使用codeclib。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>

sys.getdefaultencoding()顺便说一句,如果我的输出是 UTF8 ,为什么它指向 cp1251.py ?

        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)
4

1 回答 1

0

您在未指定编码的情况下打开了输出文件,因此使用默认系统编码;这里是 CP1251。

Jinja 模板结果生成一个 Unicode 字符串,需要对其进行编码,但默认系统编码不支持生成的代码点。

解决方案是选择一个显式编解码器。如果您正在生成 XML,则 UTF-8 是默认编码并且可以处理所有 Unicode:

with open(xml_path_upload, 'w', encoding='utf8') as writefile:
     writefile.write(template.render(**values))
于 2014-05-19T13:25:46.530 回答