0

I have a basic script that reads a csv file and I pull the specific column I want. The only challenge I have now is how to grab the specific row in that column.

Below is the code I have now.

import csv
with open('CopyofNetflowExporters-v1.5-csv.csv') as csvfile:
csvitem=csv.reader(csvfile)
#csvitem = csv.DictReader(csvfile)
for row in csvitem:
    print(row[11])
2
  • And ? How are we supposed to know which row you want exactly ? Commented Aug 23, 2018 at 14:57
  • you should look into pandas, it's very helpful for stuff like this pandas.pydata.org Commented Aug 23, 2018 at 15:05

2 Answers 2

1

You can try like below

import csv
with open('CopyofNetflowExporters-v1.5-csv.csv') as csvfile:
     csvitem=list(csv.reader(csvfile))
     print(csvitem[11])
Sign up to request clarification or add additional context in comments.

Comments

0

What you get in csvitem is a list of lists, where the first index is a row number, and the second index is a column number

You are not grabbing a column in your example. You are printing nth element of each row. To have column as an indexable object you need either to append to a list instead of prinitng in your example (that will only give you that one column as a list) or you can transpose the entire contents of the file, which is more interesting and which is shown below

transposed_csv = list(zip(*csvitem))
# now you have a list of columns, while each column is a tuple of strings
print(transposed_csv[2])

2 Comments

This was it and thank you very much! So I added the column I wanted and row with the addition of this extra snippet to it.
import csv with open('CopyofNetflowExporters-v1.5-csv.csv') as csvfile: csvitem=csv.reader(csvfile) transposed_csv = list(zip(*csvitem)) print(transposed_csv[11][3])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.