为什么不是read_fwf()输出正确的文件内容?
问题描述:
这是文件内容(为sample.txt)为什么不是read_fwf()输出正确的文件内容?
gvkeyx from thru conm gvkey co_conm co_tic
123453 19661214 19890426 S&P 500 Comp-Ltd 010490 TEXAS EASTERN CORP PEL4
123453 19670101 . S&P 500 Comp-Ltd 001078 ABBOTT LABORATORIES ABT
123453 19670101 . S&P 500 Comp-Ltd 001300 HONEYWELL INTERNATIONAL INC HON
123453 19670101 . S&P 500 Comp-Ltd 001356 ALCOA INC AA
123453 19670101 . S&P 500 Comp-Ltd 001408 FORTUNE BRANDS INC FO
我输入的代码来阅读:
In [16]: colspecs = [(0, 9), (10, 21), (22, 33), (34, 53), (54, 63), (64, 92), (93, 99)]
In [17]: df = read_fwf('sample.txt', colspecs = colspecs, header=None, index_col=None)
In [18]: df[:2]
Out[19]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data Columns:
X.1 2 non-null values
X.2 2 non-null values
X.3 2 non-null values
X.4 2 non-null values
X.5 2 non-null values
X.6 2 non-null values
X.7 2 non-null values
dtypes: object(7)
我无法理解这种输出作为其从文件完全不同。任何意见和建议都会有所帮助。由于
答
参见:http://pandas.pydata.org/pandas-docs/stable/dsintro.html#console-display
它打印的汇总,因为数据太宽了,你的终端。这可以使用pandas.set_printoptions
进行配置。你几乎肯定需要指定header=0
(这是我相信的默认值),所以df = read_fwf('sample.txt', colspecs=colspecs)
应该就足够了。