[英]Python add Unicode characters at the start of a file
I use a script to update the version of each AssemblyVersion.cs file of a .NET project. 我使用脚本来更新.NET项目的每个AssemblyVersion.cs文件的版本。 It always worked perfectly, but since a format my PC, it adds unicode character at the start of each .cs file edited.
它始终可以完美运行,但是由于格式化了我的PC,因此在每个已编辑的.cs文件的开头添加了unicode字符。 Like this:
像这样:
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
I use this code to open a file: 我使用以下代码打开文件:
with open(fname, "r") as f:
out_fname = fname + ".tmp"
out = codecs.open(out_fname, "w", encoding='utf-8')
textInFile=""
for line in f:
textInFile += (re.sub(pat, s_after,line))
out.write(u'\uFEFF')
out.write(textInFile)
out.close()
os.remove(fname)
os.rename(out_fname, fname)
I've also tried, as wrote here , to use io
instead of codecs
, but nothing is changed. 如本文所述 ,我还尝试使用
io
代替codecs
,但没有任何改变。
On other teammates' PCs it works with the same configuration (Win10 and IronPython 2.7). 在其他队友的PC上,它可以使用相同的配置(Win10和IronPython 2.7)。
What can I try to solve this issue? 我可以尝试解决什么问题? Where can I looking for the problem?
我在哪里可以找到问题?
Thanks 谢谢
It seems that the files at your file system are using ISO-8859-1 encoding, while you are adding the UT8 BOM marker at the beginning of each file. 您在每个文件的开头添加UT8 BOM标记时,文件系统中的文件似乎正在使用ISO-8859-1编码。
After your code does it's job, you get a file with UTF-8 BOM + ISO-8859-1 meta at the beginning. 代码完成工作后,您将在开头获得带有UTF-8 BOM + ISO-8859-1元的文件。
I would check the encoding of your input files before modification with Notepad++ (or any other editor) just to see if the scenario I described is valid. 在使用Notepad ++(或任何其他编辑器)进行修改之前,我将检查输入文件的编码,以查看我描述的方案是否有效。 If it is, you will need to read your input files with a different encoding in order to avoid the metadata:
如果是这样,您将需要使用不同的编码来读取输入文件以避免元数据:
with open(fname, "r", "ISO-8859-1") as f:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.