SPSS python - 将 Unicode 写入 spss 语法文件
SPSS python - writing Unicode to spss syntax file
我正在使用 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…
是否有解决此问题的快速方法,仅使用 python 2.7 的本机模块?如果可能的话,我宁愿不将所有 unicode 转换为它们的其他编码对应字符
我知道当您 SAVE AS
SPSS 中的语法文件时,有一个编码选项(Unicode (UTF-8)
与 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.
我正在使用 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…
是否有解决此问题的快速方法,仅使用 python 2.7 的本机模块?如果可能的话,我宁愿不将所有 unicode 转换为它们的其他编码对应字符
我知道当您 SAVE AS
SPSS 中的语法文件时,有一个编码选项(Unicode (UTF-8)
与 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.