简体   繁体   中英

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...

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])))

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)))

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 ! 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:

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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