Python 难以导入 .dat 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27413843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difficulty importing .dat file
提问by zsljulius
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
我以某种方式在使用 pandas read_table 函数将此文件读入 python 时遇到了困难。 http://www.ssc.wisc.edu/~bhansen/econometrics/invest.dat
This is my code:
这是我的代码:
pd.read_table(f,skiprows=[0], sep="")
Which yields error:
这会产生错误:
TypeError: ord() expected a character, but string of length 0 found
采纳答案by Marcin
Dont know about read_table, but you can read this file directly as follows:
不知道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)
Prints:
印刷:
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
...
...
The same can be obtained as follows:
同样可以如下获得:
df = pd.read_csv('/tmp/invest.dat', sep='\s+', header=None, skiprows=1)