用python读取stata文件及写入and注意事项

时间:2021-02-16 03:26:19

读取:

由于stata没有专门模块,是从pandas里面调用,官方文档少之又少,故去查看源代码

#!/usr/bin/env python
# -*- coding:utf-8 -*- from pandas.io.stata import StataReader, StataWriter filename_all = r"/opt/code/my_code/testStata/test.dta" # stata_data = StataReader(filename_all, convert_categoricals=False, encoding='utf-8')
# stata_data = StataReader(filename_all, encoding='utf8')
stata_data = StataReader(filename_all, convert_categoricals=False) data = stata_data.read() varlist = stata_data.varlist value_labels = stata_data.value_labels() fmtlist = stata_data.fmtlist variable_labels = stata_data.variable_labels() print(data)
print(varlist)
print(value_labels)
print(fmtlist)
print(variable_labels) writer = StataWriter(fname='mytest_1.dta', data=data, variable_labels=variable_labels)
writer.write_file() # 注意:
# 在写入的时候
# 没有value_labels这个选项
# variable_labels选项数据必须是latin-1的字符集,否则报错
# 并且不能encoding=‘utf-8’
# 但是2.7这些都有, 但3.5已经封装程statawriter, 而2.7里面是df.to_stata

stataWrier。。。。暂时没写呢

注意事项

1. 当用windows的时候注意路径前面加: r

2. 注意编码方式,encoding:utf-8

3. 时间格式那里

4. 还有个啥来着,忘了