简体   繁体   English

使CSV文件更容易在Python中使用标题/列名进行修改/导航?

[英]Making CSV files easier to modify/navigate within Python using headers / column names?

SO, 所以,

I have a CSV file with a varying number of columns which I think means that the traditional means of making headers as I have attempted below won't work... 我有一个CSV文件,其中包含不同数量的列, 我认为这意味着我在下面尝试过的制作标头的传统方法将无法正常工作...

The reason I want some headers is because it gets incredibly difficult to use csv files with 100+ columns and do things like rPol = math.exp((((-1.2359386)+float(row[48])*(row[33])+row[29]+row[13]+row[97]/row[50]))) 我想要一些标题的原因是,使用带有100多个列的csv文件并执行rPol = math.exp((((-1.2359386)+float(row[48])*(row[33])+row[29]+row[13]+row[97]/row[50])))

Trying to remember the identity of each row is a nussance, and it would be much easier if I could do something like: rPol = math.exp((((-1.2359386)+float(depolscore)*(master_num)+ripen+offset_score+full/epilic))) 尝试记住每一行的标识是一件很麻烦的事,如果我可以做以下事情会更容易: rPol = math.exp((((-1.2359386)+float(depolscore)*(master_num)+ripen+offset_score+full/epilic)))

import csv

reader = csv.reader(open("test.csv"), delimiter=",")
headers = {"data", "title", "here", "testing", "stackoverflow"}
csv.DictWriter(reader, headers)
reader.writeheader()
for row in reader:
    print testing

How would I go about giving specific columns a header without doing something like this: 我该如何在不执行以下操作的情况下为特定的列提供标题:

for row in reader:
    # put the columns into variables...
    data = row[0]
    title = row[1]
    here = row[2]
    testing = row[3]
    stackoverflow = row[4]

    # Do math
    score = data * here / stackoverflow
    # Print for user sake
    print score
    # Change the testing value
    testing = testing + (score - title)

    # Put values back into the reader object?
    row[0] = data
    row[1] = title
    row[2] = here
    row[3] = testing
    row[4] = stackoverflow

Any ideas? 有任何想法吗?

You could try using a namedtuple ! 您可以尝试使用namedtuple It's a subclass of tuple, allowing for easy creation from an iterable and easy access to fields by name. 它是元组的子类,可通过名称的可迭代访问轻松地创建字段。 The only gotcha you should be aware of is that namedtuples, like tuples, are immutable so you'd have to store the new tuples somewhere: 您应该知道的唯一陷阱是,像元组一样,namedtuple是不可变的,因此您必须将新元组存储在某个地方:

headers = ["data", "title", "here", "testing", "stackoverflow"]
Row = namedtuple('Row', headers)
for raw_row in reader:
    row = Row._make(raw_row)

    # Do math
    score = row.data * row.here / stackoverflow
    # Print for user sake
    print score
    # Change the testing value
    new_testing = row.testing + (score - row.title)
    new_row = row._replace(testing=new_testing)

    # Do something with new_row...

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

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