简体   繁体   English

在Python中读取csv文件时如何定义列标题

[英]How to define column headers when reading a csv file in Python

I have a comma separated value table that I want to read in Python. 我有一个逗号分隔值表,我想用Python阅读。 What I need to do is first tell Python not to skip the first row because that contains the headers. 我需要做的是首先告诉Python不要跳过第一行,因为它包含标题。 Then I need to tell it to read in the data as a list and not a string because I need to build an array out of the data and the first column is non-integer (row headers). 然后我需要告诉它将数据读入列表而不是字符串,因为我需要从数据中构建一个数组,并且第一列是非整数(行标题)。

There are a total of 11 columns and 5 rows. 总共有11列和5行。
Here is the format of the table (except there are no row spaces): 这是表的格式(除了没有行空格):

col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11

w0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10        
w1  1, 2, 3, 4, 5, 6, 7, 8, 9, 10    
w2  1, 2, 3, 4, 5, 6, 7, 8, 9, 10   
w3  1, 2, 3, 4, 5, 6, 7, 8, 9, 10 

Is there a way to do this? 有没有办法做到这一点? Any help is greatly appreciated! 任何帮助是极大的赞赏!

You can use the csv module for this sort of thing. 您可以使用csv模块进行此类操作。 It will read in each row as a list of strings representing the different fields. 它将在每一行中读取表示不同字段的字符串列表。

How exactly you'd want to use it depends on how you're going to process the data afterwards, but you might consider making a Reader object (from the csv.reader() function), calling next() on it once to get the first row, ie the headers, and then iterating over the remaining lines in a for loop. 你究竟想要如何使用它取决于你之后如何处理数据,但是你可以考虑制作一个Reader对象(来自csv.reader()函数),在它上面调用next()一次得到第一行,即标题,然后迭代for循环中的其余行。

r = csv.reader(...)
headers = r.next()
for fields in r:
    # do stuff

If you're going to wind up putting the fields into a dict, you'd use DictReader instead (and that class will automatically take the field names from the first row, so you can just construct it an use it in a loop). 如果你打算将字段放入dict中,你可以使用DictReader (该类将自动从第一行获取字段名称,因此你可以构建它在循环中使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM