Python - 使用 2 种定义类型处理 csv

标签 python csv numpy

如何处理同一文件中具有两个重复定义的 csv 文件。

我的意思是,随着比赛次数的增加和每场比赛的多匹马的增加,该文件看起来像这样重复。

Meeting,05/07/14,RHIL,Rosehill Gardens,Weights,TAB,+3m Entire Circuit,          ,
Race,1,CIVIC STAKES,CIVIC,CIVIC,1350,~         ,3U        ,~         ,QLT       ,54,0,0,5/07/2014,,          ,          ,          ,          ,No class restriction, Quality, For Three-Years-Old and Upwards, No sex restriction, (Listed),Of $100000. First $60000, second $20000, third $10000, fourth $5000, fifth $2000, sixth $1000, seventh $1000, eighth $1000
Horse,1,Bennetta,0,"Grahame Begg",Randwick,,0,0,16-3-1-3 $390450.00,,0,0,0,,98.00,M,
Horse,2,Breakfast in Bed,0,"David Vandyke",Warwick Farm,,0,0,20-6-1-5 $201250.00,,0,0,0,,81.00,M,
Horse,3,Capital Commander,0,"Gerald Ryan",Rosehill,,0,0,43-9-9-3 $438625.00,,0,0,0,,85.00,M,
Horse,4,Coup Ay Tee (NZ),0,"Chris Waller",Rosehill,,0,0,35-9-6-5 $519811.00,,0,0,0,,101.00,G,
Horse,5,Generalife,0,"John O'Shea",Warwick Farm,,0,0,19-6-1-3 $235045.00,,0,0,0,,87.00,G,
Horse,6,He's Your Man (FR),0,"Chris Waller",Rosehill,,0,0,13-2-3-1 $108110.00,,0,0,0,,93.00,G,
Horse,7,Hidden Kisses,0,"Chris Waller",Rosehill,,0,0,40-8-8-5 $565750.00,,0,0,0,,96.00,M,
Horse,8,Oakfield Commands,0,"Gerald Ryan",Rosehill,,0,0,22-7-4-6 $269530.00,,0,0,0,,94.00,G,
Horse,9,Taxmeifyoucan,0,"Gregory Hickman",Warwick Farm,,0,0,18-2-4-4 $539730.00,,0,0,0,,91.00,G,
Horse,10,The Peak,0,"Bart & James Cummings",Randwick,,0,0,15-6-1-0 $426732.00,,0,0,0,,95.00,G,
Horse,11,Tougher Than Ever (NZ),0,"Chris Waller",Rosehill,,0,0,17-3-2-3 $321613.00,,0,0,0,,97.00,H,
Horse,12,TROMSO,0,"Chris Waller",Rosehill,,0,0,47-8-11-2 $622300.00,,0,0,0,,103.00,G,
Race,2,FLYING WELTER - BENCHMARK 95 HCP,BM95,BM95,1100,BM95      ,3U        ,~         ,HCP       ,54,0,0,5/07/2014,,          ,          ,          ,          ,BenchMark 95, Handicap, For Three-Years-Old and Upwards, No sex restriction,Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000
Horse,1,Big Bonanza,0,"Don Robb",Wyong,,0,57.5,31-9-4-3 $366860.00,,0,0,0,,92.00,G,
Horse,2,Casual Choice,0,"Joseph Pride",Warwick Farm,,0,54,8-2-3-0 $105930.00,,0,0,0,,80.00,G,
Horse,3,Cradle Me,0,"David Pfieffer",Warwick Farm,,0,54,28-7-4-2 $268215.00,,0,0,0,,89.00,M,

我可以使用阅读器以标准方式解析它,并且它有义务。

In [9]: with open('/home/sayth/Scripts/test.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    for row in f_csv:
        print row

['Race', '1', 'CIVIC STAKES', 'CIVIC', 'CIVIC', '1350', '~         ', '3U        ', '~         ', 'QLT       ', '54', '0', '0', '5/07/2014', '', '          ', '          ', '          ', '          ', 'No class restriction', ' Quality', ' For Three-Years-Old and Upwards', ' No sex restriction', ' (Listed)', 'Of $100000. First $60000', ' second $20000', ' third $10000', ' fourth $5000', ' fifth $2000', ' sixth $1000', ' seventh $1000', ' eighth $1000']
['Horse', '1', 'Bennetta', '0', 'Grahame Begg', 'Randwick', '', '0', '0', '16-3-1-3 $390450.00', '', '0', '0', '0', '', '98.00', 'M', '']
['Horse', '2', 'Breakfast in Bed', '0', 'David Vandyke', 'Warwick Farm', '', '0', '0', '20-6-1-5 $201250.00', '', '0', '0', '0', '', '81.00', 'M', '']

然而,种族的定义和其中包含的信息根本不是包含 38 项的马的标题。它实际上是与马 table 相连的自己的 table 。

我一直在阅读这里的示例 Python Cookbook它们很棒,但假设一个带有标准标题和数据的 csv 文件。

使用 numpy 和 genfromtxt 仅报告文件中的所有错误,例如。

np.genfromtxt('/home/sayth/Scripts/test.csv',)
ValueError: Some errors were detected !
    Line #2 (got 38 columns instead of 5)
    Line #3 (got 3 columns instead of 5)
    Line #4 (got 6 columns instead of 5)
    Line #5 (got 4 columns instead of 5)
    Line #6 (got 6 columns instead of 5)
    Line #7 (got 4 columns instead of 5)
    Line #8 (got 6 columns instead of 5)
    Line #9 (got 4 columns instead of 5)
    Line #10 (got 4 columns instead of 5)
...

我应该如何处理这样的文件以保持种族和马匹之间的关系?

最佳答案

不确定具体细节,但我会从这样的事情开始......

races = []
racefound = False
with open('/home/sayth/Scripts/test.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    for row in f_csv:
        if row[0] == 'Race':
            if racefound:
                races.append(raceinfo)
                raceinfo = {'info': row, 'horses': horses}
                racefound = True
            horses = []
        else:
            horses.append(row)

更新:

如果您想使用比赛行作为标题信息,这样的内容可能更符合您的需求..

with open('/home/sayth/Scripts/test.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    for row in f_csv:
        if row[0] == 'Race':
            raceinfo = row
        else:
            print dict(zip(raceinfo,row))

关于Python - 使用 2 种定义类型处理 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24502202/

相关文章:

python - 使用 numpy cumsum 计算求和面积表的 3D 变体

python - 叠加图并独立滚动 matplotlib

python - 从模块名称获取路径

python - 如何获取空python数组中的dataFrame数组值

php - 使用 PHP 从 MySQLi 生成 CSV 文件正在保存但不要求下载

postgresql - 将 CSV 文件导入 PostgreSQL

MySQL通过字符串加载CSV文件

python - 使用不同大小的 h5py 数组保存

python - 寻找一种有效的方法来操作 JSON 数据

python - Python 中的嵌套列表理解