0

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?

3
  • 1
    Have you tried campaign[3]? Don't forget that Python uses 0-based indexing. Commented Jul 30, 2018 at 20:20
  • yupper sure did, print(campaign[2]) IndexError: list index out of range Commented Jul 30, 2018 at 20:21
  • What are you hoping to gain from your iteration? If you use "for row in inputcsv: print(row)" you'll see that you're really only getting the column headers. Are you trying to iterate over rows? inputcsv.itertuples() might be what you want to use. Commented Jul 30, 2018 at 20:28

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I understand that, it was a typo in my question. But print(campaign[3]) results in the same error IndexError: list index out of range
0

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

3 Comments

this results in [ 'exact', 'find a doctor', 'Non-Branded Brand (Footprint)', 'Find Doctor - Exact'] which gives me the whole row. Would I assign this to an object and then access it that way. So, something like, list = data[3] and then list[2]
Nowhere in your original data the word 'Doctor' or 'Footprint' appear, so it seems you didn't provide the data you are actually using. Please ensure your example is self-contained and reproduces the problematic behavior.
I also updated the answer with the answer to your question - yes, data is the whole row, if you want to access an element of that, index again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.