python读取dat文件内容_python – 使用Pandas读取数据(.dat文件)

您可以使用带有列顺序的参数usecols:

import pandas as pd

from pandas.compat import StringIO

temp=u"""TIME XGSM

2004 006 01 00 01 37 600 1

2004 006 01 00 02 32 800 5

2004 006 01 00 03 28 000 8

2004 006 01 00 04 23 200 11

2004 006 01 00 05 18 400 17"""

#after testing replace StringIO(temp) to filename

df = pd.read_csv(StringIO(temp),

sep="\s+",

skiprows=1,

usecols=[0,7],

names=['TIME','XGSM'])

print (df)

TIME XGSM

0 2004 1

1 2004 5

2 2004 8

3 2004 11

4 2004 17

编辑:

您可以使用分隔符regex – 2和更多空格,然后添加engine =’python’,因为警告:

ParserWarning: Falling back to the ‘python’ engine because the ‘c’ engine does not support regex separators (separators > 1 char and different from ‘\s+’ are interpreted as regex); you can avoid this warning by specifying engine=’python’.

import pandas as pd

from pandas.compat import StringIO

temp=u"""TIME XGSM

2004 006 01 00 01 37 600 1

2004 006 01 00 02 32 800 5

2004 006 01 00 03 28 000 8

2004 006 01 00 04 23 200 11

2004 006 01 00 05 18 400 17"""

#after testing replace StringIO(temp) to filename

df = pd.read_csv(StringIO(temp), sep=r'\s{2,}', engine='python')

print (df)

TIME XGSM

0 2004 006 01 00 01 37 600 1

1 2004 006 01 00 02 32 800 5

2 2004 006 01 00 03 28 000 8

3 2004 006 01 00 04 23 200 11

4 2004 006 01 00 05 18 400 17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值