我这里拥有一个利用unicode编码的txt文件,
当我采用下列代码读取文件时(代码中省略了部分文件路径)
with open('STK_MKT_ValuationMetrics.txt','r') as f:
pettmInfo = pd.read_table(f)
这种代码读取时产生了报错:
'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
然后我分别采用了
1.with open('STK_MKT_ValuationMetrics.txt','rb') as f:
报错:'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
2.with open('STK_MKT_ValuationMetrics.txt','rb',encoding='utf-8') as f:
报错:binary mode doesn't take an encoding argument
3.with open('STK_MKT_ValuationMetrics.txt','r',encoding='utf-8') as f:
报错:'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
无一例外都失败了,可这都是根据报错一步一步百度搜来的做法。。。。
然后我就把txt文档另存为utf-8编码的,再次读取:
with open('STK_MKT_ValuationMetrics.txt','r',encoding='utf-8') as f:
pettmInfo = pd.read_table(f)
这时没有报错,但是第一列有些地方以0开头缺都被省略了,如000006变成了6,这并不是我想要的结果,如下图:
请问这该如何解决呢?如果解答能只对原文件进行读取不改变编码格式我会觉得更好,实在不行的话我也就只能手动转化为utf-8读取了,那么这些省略的0我该如何处理呢?谢谢解答!