[英]Python method or pre-existing module to access csv via headers instead of column ID's
I am being forced to work a project off of CSV files instead of a database... irritating but true. 我被迫从一个CSV文件而不是一个数据库中进行项目工作……很烦人,但事实如此。 I have no control of the organization which the CSV will come out in. I can reasonably guarantee that the names will be maintained in the CSV header. 我无法控制CSV的发布组织。我可以合理地保证名称将保留在CSV标头中。
I was just getting ready to write some code to return column id's on string matches, but was wondering if there was a module that might be able to do this for me? 我只是准备编写一些代码以返回字符串匹配中的列ID,但是想知道是否有一个模块可以为我执行此操作?
e.g.
data = csv.csvRowData[5] becomes
data = csv.csvRowData[find_rowID('column_name')]
Forgive me if my code syntax is off, came from php. 如果我的代码语法不正确,请原谅我,它来自php。 Will figure out how to make it work in the syntax. 将弄清楚如何使其在语法中起作用。
I use the pandas
package, there is a powerful read_csv
utility http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html 我使用pandas
软件包,有一个功能强大的read_csv
实用程序http://pandas.pydata.org/pandas-docs/stable/generation/pandas.io.parsers.read_csv.html
cat test.csv
date,value
2014,Hi
2015,Hello
import pandas as pd
df = pd.read_csv('test.csv')
This returns a pandas.DataFrame
that does what you want (and a lot more, eg conversion of the data types on the columns), try it out on IPython: 这将返回一个pandas.DataFrame
,它会执行您想要的操作(以及更多操作,例如,转换列上的数据类型),请在IPython上进行尝试:
In [5]: df['date']
Out[5]:
0 2014
1 2015
Name: date, dtype: int64
In [6]: df.columns
Out[6]: Index([u'date', u'value'], dtype='object')
The python standard library includes the csv module . python标准库包括csv模块 。
It provides the DictReader class which will allow you to access a row's data by column header labels. 它提供了DictReader类,该类允许您通过列标题标签访问行的数据。
DictReader will take the first row in the CSV file to be the column headers then provide every subsequent row as a dict with the column labels as keys and the row's data as values. DictReader将CSV文件中的第一行作为列标题,然后将随后的每一行作为dict提供,其中列标签作为键,而行的数据作为值。
For example if people.csv
looked like this: 例如,如果people.csv
看起来像这样:
"First Name","Last Name"
Peter,Venkman
Egon,Spengler
You can use DictReader like this: 您可以像这样使用DictReader:
import csv
with open('people.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
print row["Last Name"]
# will output
Venkman
Spengler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.