简体   繁体   English

如何使用Python逐列读取CSV文件

[英]How to read CSV file column by column with Python

I have a CSV file in the following format 我有以下格式的CSV文件

  Date, Company, Company, Company
1/1/12,      10,     100,      50
1/2/12,      12,      99,      53
1/3/12,      11,      97,      49

I'm trying to input the data into a PSQL database. 我正在尝试将数据输入到PSQL数据库中。

How would I go about going column by column on the data, so that I would have have something like INSERT INTO table VALUES(company, date, price); 我将如何逐列处理数据,以便拥有类似INSERT INTO table VALUES(company, date, price); ?

Each column corresponds to a company 每列对应一个公司

I'm wondering if something like this would work: 我想知道这样的事情是否会起作用:

import csv

with open("file.csv") as f:
    reader = csv.reader(f)
    for i, column in enumerate(zip(*reader)):
        if i == 0:
            _, dates = column
        else:
            # PY3.x
            company, *prices = column
            # PY2.7
            company, prices = column[0], column[1:]

            # Do SQL command here

The idea is to use to transpose the data that is read in by rows with csv.reader by using zip(*reader). 这个想法是通过使用zip(* reader)来转置使用csv.reader通过行读取的数据。 I can't test this out right now but you might have to use zip(*list(reader)) to transpose all the data, however this will load the entire file and probably make a copy. 我目前无法对此进行测试,但是您可能必须使用zip(*list(reader))来转置所有数据,但是这将加载整个文件可能进行复制。 Since your data is small that's probably ok. 由于您的数据很小,因此可以。

For this size of data you can also use pandas. 对于这种大小的数据,您还可以使用熊猫。 Which would just be something like: 就像这样:

import pandas as pd

data = pd.read_csv ("file.csv", index_col=0, parse_dates=False)

dates = data.index.values

for company in data.columns:
    price = data[company].values

    #SQL command here

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

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