简体   繁体   中英

accessing data when using csv.reader()

Here is an example of my data,

match_type search campaign group phrase physicians Branded System phrase phrase locations Branded System phrase exact find Non-Branded Brand exact

I am using csv.reader to read in a csv I have.

with open("pos_input.csv") as csvfile:
    inputcsv = csv.reader(csvfile, delimiter=',')
    for row in inputcsv:
        match = row[1:2]
        search = row[2:3]
        campaign = row[3:4]
        ad = row[4:]

I am then assigning each column in the csv to an object. For example the first column holds values about each row. So,

print(campaign)

would result in

['Campaign'] ['Branded System'] ['Branded System'] ['Non-Branded Brand']

campaign being the column header and then each string after represents a row input.

My question is, how do I access just 'Non-Branded Brand'. I have tried this,

campaign[3]

but results in an error,

IndexError: list index out of range

Do I need to do some converting here?

Looks like you goal is to separate the columns of the csv. First of all, be sure that the csv file is comma separated:

match_type,search,campaign,group
phrase,physicians,Branded System,phrase
phrase,locations,Branded System,phrase
exact,find,Non-Branded Brand,exact

Then, you need to iterate over the object returned by the csv.reader , with that object you iterate over each row of the file. In each iteration, you access each column by the respective index, starting from 0. For example, the third column is the index 2. So to save the "campaing" column, you just use the index 2. The following code saves all columns, but is not safe, because if you open a file with a inferior number of columns, a exception will be thrown, be aware.

import csv
match = []
search = []
campaign = []
group = []  
with open("pos_input.csv") as csvfile:
    inputcsv = csv.reader(csvfile, delimiter=',')
    for row in inputcsv:
        match.append(row[0])
        search.append(row[1])
        campaign.append(row[2])
        group.append(row[3])

So the "Non-Branded Brand" is the campaing[3]:

print(campaign[3])

would result:

Non-Branded Brand

In most programming languages, lists start at 0. So the first item in your list will be campaign[0] . So the fourth item, Non-Branded Brand is accessed via campaign[3] .

Your data extraction is wrong. By using slicing, you create 1-element lists for the column values. Which most certainly is not what you want. And on top of that, you do not retain the extracted values of the individual rows. So you don't have the 'Non-branded Brand'.

Use something like this instead:

with open("pos_input.csv") as csvfile:
    data = list(csv.reader(csvfile, delimiter=','))
print(data[3][2]) # to access the 4th row, 3rd column

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