用 pandas 读取rpt文件 [英] Reading rpt files with pandas

查看:96
本文介绍了用 pandas 读取rpt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令将rpt数据读取到了熊猫

I read rpt data to pandas by using:

import pandas as pd
df = pd.read_fwf("2014-1.rpt", skiprows=[1], nrows=150)

我实际上在此处 但是,对于某些列,分隔不准确.这是我得到的样本:

I actually follow the anwser here However, for some columns, seperation is not accurate. It is sample of what I get:

Country   Order Date Device   Category
UK        2014-01-03 Desktop  Shoes
IT        2014-01-03 Desktop  Shoes
FR        2014-01-04 Desktop  Dress
FR        2014-01-04 Tablet   Dress
US        2014-01-05 Desktop  Bags
US        2014-01-06 Desktop  Bags
UK        2014-01-07 Tablet   Dress

例如,它将订单日期"和设备"列读取为单个列.实际上,这只是一个例子,有很多这样的列. 怎么解决呢?你有什么主意吗? 实际上,这些有问题的列的宽度可能固定不变

For instance it reads Order Date and Device columns as a single column. Actually, it is just an example, there are many columns like this. How to solve it? Do you have any idea? Actually these columns with problems might have fixed widths

推荐答案

这个问题很旧,但这是一个答案.您可以使用熊猫将其作为csv读取.我已经将它用于各种rpt文件,并且已经奏效.

This question is old, but here is an answer. You can read it as a csv using pandas. I have used this for a variety of rpt files and it has worked.

import pandas as pd
df = pd.read_csv("2014-1.rpt", skiprows=[1], nrows=150)

这篇关于用 pandas 读取rpt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆