简体   繁体   English

Python方法或预先存在的模块通过标头(而不是列ID)访问csv

[英]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.

相关问题 根据对其他几个预先存在的列的评估,在 csv 文件中创建和 append 单列 1、0 和 -1 - Create and append single column of 1’s, 0’s, and -1’s in csv file based on assessment of several other pre-existing columns 如何在sqlalchemy中更改现有数据库表的两个不同的列标题? - How do I alter two different column headers of a pre-existing database table in sqlalchemy? Append 到 Python 中预先存在的键的值 - Append to value of pre-existing keys in Python 如何使用python向csv文件中的预先存在的行添加一个值 - How to add a add a value to a pre-existing row in a csv file using python 如何使用Python中的硬编码数据将数组的内容转储到预先存在的csv中 - How to dump contents of an array to a pre-existing csv with hardcoded data in python PyCharm:来自预先存在的方法提示的 function 的 Typedef 提示 - PyCharm: Typedef hint for function from pre-existing method's hint 使用 dataframe 中 3 个预先存在的列中的“def”编写 python function; 第 1 列和第 2 列作为输入 = 第 3 列作为 output - write a python function using ```def``` from 3 pre-existing columns in a dataframe; columns 1 and 2 as inputs = column 3 as output 根据预先存在的列在 pandas 中创建另一列 - Creating another column in pandas based on a pre-existing column python:将电子邮件地址解析为三元组的预先存在的函数? - python: a pre-existing function to parse an email address into a 3-tuple? Python 标题大小写,但保留预先存在的大写 - Python Title Case, but leave pre-existing uppercase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM