SPSS 编写 Unicode 到 spss 语法文件



我正在使用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'

如果我在记事本++中打开生成的文件,我会正确看到文本。但是,如果我以 SPSS 语法打开文件,我会看到以下内容:

some text…

有没有一种快速的方法,只使用 python 2.7 的本机模块?如果可能的话,我宁愿不将所有 unicode 转换为其他编码的相应字符

我知道当您在 SPSS 中SAVE AS语法文件时,有一个编码选项(Unicode (UTF-8) vs. Local Encoding (。

不确定这里的分辨率是多少,但尝试添加到您的 python 生成的文本文件中,在第一行:

* Encoding: UTF-8.

最后,在codecs模块的帮助下,这奏效了

begin program.
import codecs
outfile=codec.sopen("c:/temp/syntax.sps","w+","utf-8-sig")
outfile.write("some text…")
outfile.close()
end program.

最新更新