python - how to get values in csv to a report

LN41
LN41 used Ask the Experts™
on
Trying to get a report from a csv file.

csv file: servers.csv
name,date,zone
server1,3/3/2016,dmz
server2,4/3/2017,prod
server3,4/4/2017,nonprod
server4,6/6/2017,prod

I'm trying to get a list of prod servers that contains the server name and zone.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018
Commented:
Try ingesting data in your python script using this pipeline...

cat servers.csv | grep -v ^name | grep ",prod"

Open in new window

Top Expert 2014
Commented:
import csv

f = file('c:\users\mark\downloads\Q_29075243.csv')
for l in csv.reader(f):
    print l[0],l[2]

Open in new window

Produces:
name zone
server1 dmz
server2 prod
server3 nonprod
server4 prod

Open in new window

Author

Commented:
Hi aikimark,Your answer is helpful but looking for a way to filter only for prod servers rather than manually id them. ideal result produces:

name zone
server2 prod
server4 prod
Commented:
I think I got it. Thanks for the help.

import csv
with open('servers.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        if row[3] == 'prod':
            print(row[1],row[3])

Author

Commented:
figured it out myself

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial