简体   繁体   中英

Python - Trying to read a csv file and output values with specific criteria

I am going to start off with stating I am very much new at working in Python. I have a very rudimentary knowledge of SQL but this is my first go 'round with Python. I have a csv file of customer related data and I need to output the records of customers who have spent more than $1000. I was also given this starter code:

import csv  
import re  
data = []

with open('customerData.csv') as csvfile:  
    reader = csv.DictReader(csvfile)  
    for row in reader:  
        data.append(row)  
print(data[0])  
print(data[1]["Name"])  
print(data[2]["Spent Past 30 Days"])  

I am not looking for anyone to give me the answer, but maybe nudge me in the right direction. I know that it has opened the file to read and created a list (data) and is ready to output the values of the first and second row. I am stuck trying to figure out how to call out the column value without limiting it to a specific row number. Do I need to make another list for columns? Do I need to create a loop to output each record that meets the > 1000 criteria? Any advice would be much appreciated.

To get a particular column you could use a for loop. I'm not sure exactly what you're wanting to do with it, but this might be a good place to start.

for i in range(0,len(data)):
    print data[i]['Name']

len(data) should equal the number of rows, thus iterating through the entire column

The sample code does not give away the secret of data structure. It looks like maybe a list of dicts. Which does not make much sense, so I'll guess how data is organized. Assuming data is a list of lists you can get at a column with a list comprehension:

data = [['Name','Spent Past 30 Days'],['Ernie',890],['Bert',1200]]
spent_column = [row[1] for row in data]
print(spent_column)  # prints: ['Spent Past 30 Days', 890, 1200]

But you will probably want to know who is a big spender so maybe you should return the names:

data = [['Name','Spent Past 30 Days'],['Ernie',890],['Bert',1200]]
spent_names = [row[0] for row in data[1:] if int(row[1])>1000]
print(spent_names)  # prints: ['Bert']

If the examples are unclear I suggest you read up on list comprehensions; they are awesome :)

You can do all of the above with regular for-loops as well.

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