我正在为Python项目创建自己的cookiecutter template,该项目主要以utf-8编码。但它包含以iso-8859-1(latin1)编码的.ini
和.php
个文件。此资源必须以latin1编码,因为它是遗留代码的一部分。
当我跑步时:
cookiecutter cookiecutter-mytemplate # <- directory of my project
我在代码生成过程中遇到以下错误:
Traceback (most recent call last):
...
File ".../lib/python2.7/site-packages/cookiecutter/cli.py", line 123, in main
default_config=default_config,
File ".../lib/python2.7/site-packages/cookiecutter/main.py", line 91, in cookiecutter
output_dir=output_dir
File ".../lib/python2.7/site-packages/cookiecutter/generate.py", line 349, in generate_files
generate_file(project_dir, infile, context, env)
File ".../lib/python2.7/site-packages/cookiecutter/generate.py", line 166, in generate_file
tmpl = env.get_template(infile_fwd_slashes)
File ".../lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File ".../lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File ".../lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File ".../lib/python2.7/site-packages/jinja2/loaders.py", line 175, in get_source
contents = f.read().decode(self.encoding)
File ".../lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 308: invalid continuation byte
当然,结果是部分生成的,并且在解析iso-8859-1文件时被中断(内部带有“é”)。
我可以使用前/后挂钩在模板生成之前将我的资源转换为utf-8,然后将它们转换回iso-8859-1吗?怎么样?
有没有办法处理非utf-8文件?
答案 0 :(得分:0)
最后,我将所有int main() {
foo();
}
和what()
文件存储在utf-8中,并使用钩子在后代中转换iso-8859-1中的文件。
以下是.ini
的代码:
.php