难以导入 .dat 文件 [英] Difficulty importing .dat file
本文介绍了难以导入 .dat 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 pandas read_table 函数将这个文件读入 python 时遇到了困难.http://www.ssc.wisc.edu/~bhansen/econometrics/invest.dat
I am somehow having difficulty reading in this file into python with pandas read_table function. 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
推荐答案
不知道read_table,但是可以直接读取这个文件如下:
Dont know about read_table, but you can read this file directly as follows:
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)
这篇关于难以导入 .dat 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文