【问题标题】:How to skip pre header lines with csv.DictReader?如何使用 csv.DictReader 跳过前标题行?
【发布时间】:2011-11-27 03:43:00
【问题描述】:

我想csv.DictReader 从文件中推断出字段名称。 The docs say “如果省略 fieldnames 参数,则 csvfile 第一行中的值将用作字段名。”,但在我的情况下,第一行包含标题和第二行包含名称的行。

我无法按照Python 3.2 skip a line in csv.DictReader 申请next(reader),因为字段名分配是在初始化阅读器时发生的(或者我做错了)。

csv 文件(从 Excel 2010 导出,original source):

CanVec v1.1.0,,,,,,,,,^M
Entity,Attributes combination,"Specification Code
Point","Specification Code
Line","Specification Code
Area",Generic Code,Theme,"GML - Entity name
Shape - File name
Point","GML - Entity name
Shape - File name
Line","GML - Entity name
Shape - File name
Area"^M
Amusement park,Amusement park,,,2260012,2260009,LX,,,LX_2260009_2^M
Auto wrecker,Auto wrecker,,,2360012,2360009,IC,,,IC_2360009_2^M

我的代码:

f = open(entities_table,'rb')
try:
    dialect = csv.Sniffer().sniff(f.read(1024))
    f.seek(0)

    reader = csv.DictReader(f, dialect=dialect)
    print 'I think the field names are:\n%s\n' % (reader.fieldnames)

    i = 0
    for row in reader:
        if i < 20:
            print row
            i = i + 1

finally:
    f.close()

当前结果:

I think the field names are:
['CanVec v1.1.0', '', '', '', '', '', '', '', '', '']

想要的结果:

I think the field names are:
['Entity','Attributes combination','"Specification Code Point"',...snip]

我意识到简单地删除第一行并继续操作会很方便,但我正在尝试尽可能接近就地读取数据并尽量减少人工干预。

【问题讨论】:

    标签: python csv


    【解决方案1】:

    f.seek(0)之后,插入:

    next(f)
    

    在初始化DictReader之前将文件指针前进到第二行。

    【讨论】:

    • 哇!当然。非常感谢您对初学者的耐心等待。
    【解决方案2】:

    我使用了来自 itertools 的 islice。我的标题在大序言的最后一行。我已通过序言并使用 hederline 作为字段名:

    with open(file, "r") as f:
        '''Pass preamble'''
        n = 0
        for line in f.readlines():
            n += 1
            if 'same_field_name' in line: # line with field names was found
                h = line.split(',')
                break
        f.close()
        f = islice(open(i, "r"), n, None)
    
        reader = csv.DictReader(f, fieldnames = h)
    

    【讨论】:

    • 这是一种更灵活的解决方案,前提是您可以确定地知道一个字段名称(合理的预期)。谢谢。
    猜你喜欢
    • 2013-12-09
    • 2012-12-18
    • 2011-06-14
    • 2015-02-26
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    相关资源
    最近更新 更多