我正在使用python编写一个文本到一个.sps文件(这是SPSS语法文件)。
begin program.
outfile=open("c:/temp/syntax.sps","w+")
outfile.write("some text…")
outfile.close()
end program.文本中的最后一个字符是:
>>> my_text="some text…"
>>> my_text[-1]
'\x85'如果我在Notepad++中打开结果文件,我将正确地看到文本。但是,如果我用SPSS语法打开该文件,我会看到以下内容:
some text…是否有一种快速的方法,只使用python2.7的本机模块?如果可能的话,我宁愿不把所有的独角兽转换成其他编码的相应字符。
发布于 2017-11-27 15:56:30
最后,在codecs模块的帮助下,这起了作用。
begin program.
import codecs
outfile=codec.sopen("c:/temp/syntax.sps","w+","utf-8-sig")
outfile.write("some text…")
outfile.close()
end program.发布于 2017-11-20 17:27:40
我知道,当你SAVE AS一个语法文件在SPSS中,有一个选项编码(Unicode (UTF-8)与Local Encoding)。
不确定这里的解析是什么,但是尝试将第一行添加到您生成的python文本文件中:
* Encoding: UTF-8.
https://stackoverflow.com/questions/47397583
复制相似问题