难以导入.dat文件

时间:2014-12-11 01:20:37

标签: python pandas

我在某种程度上难以使用pandas read_table函数将此文件读入python。 http://www.ssc.wisc.edu/~bhansen/econometrics/invest.dat

这是我的代码:

pd.read_table(f,skiprows=[0], sep="")

产生错误:

TypeError: ord() expected a character, but string of length 0 found

1 个答案:

答案 0 :(得分:21)

不了解read_table,但您可以直接阅读此文件,如下所示:

import pandas as pd    

with open('/tmp/invest.dat','r') as f:
    next(f) # skip first row
    df = pd.DataFrame(l.rstrip().split() for l in f)

print(df)

打印:

              0            1             2            3
0     17.749000   0.66007000    0.15122000   0.33150000
1     3.9480000   0.52889000    0.11523000   0.56233000
2     14.810000    3.7480300    0.57099000   0.12111000
...
...

同样可以获得如下:

df = pd.read_csv('/tmp/invest.dat', sep='\s+', header=None, skiprows=1)