
以下内容在3.3中为我工作:
>>> import numpy as np, pandas as pd>>> import io, pkgutil>>> wells = pkgutil.get_data('pymc.examples', 'data/wells.dat')>>> type(wells)<class 'bytes'>>>> df = pd.read_csv(io.BytesIO(wells), encoding='utf8', sep=" ", index_col="id", dtype={"switch": np.int8})>>> df.head() switch arsenic dist assoc educid 1 1 2.36 16.826000 0 02 1 0.71 47.321999 0 03 0 2.07 20.966999 0 104 1 1.15 21.486000 0 125 1 1.10 40.874001 1 14[5 rows x 5 columns]注意:我必须手动将其放置
wells.dat在该位置,所以我不能保证我已正确复制了它,并且没有终端空格,因为我删除了一些空格。但经过
read_csv一个
BytesIO对象,编码参数应该工作。(实际上,没有它,您可能会逃脱,但这是一个好习惯。
io.TextIOWrapper可能是另一种选择。)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)