praiseHellRaiseDale
praiseHellRaiseDale

Reputation: 2270

Why do I need to add quotes to this csv File?

I have a csv file I'm trying to parse, and I'm getting a list index out of range error when I run my program.

Here's the original csv file:

test.csv

Date, Time To Process
10/26/2017 7:57:28 PM, 5
10/26/2017 7:57:46 PM, 3
10/26/2017 7:57:47 PM, 1
10/26/2017 7:57:49 PM, 1
10/26/2017 7:57:50 PM, 6
10/26/2017 7:57:52 PM, 5

And here's my code:

import csv

with open('test.csv', 'rb') as n:
    has_header = csv.Sniffer().has_header(n.read(1024))
    n.seek(0)
    reader = csv.reader(n)
    if has_header:
        next(reader)
    dates = []
    timeToProcess = []
    for row in reader:
        print row
        values = row[0].split(',')
        dates.append(values[0])
        timeToProcess(values[1])

This will just print the first row of the csv file, and then output the error timeToProcess.append(values[1]) IndexError: list index out of range

If I change the csv file to have quotes around each row, everything works as expected. Why is that, and is there a way for this program to run without the quotes in the csv file?

test.csv (The one that works, with the quotes)

Date, Time To Process
"10/26/2017 7:57:28 PM, 5"
"10/26/2017 7:57:46 PM, 3"
"10/26/2017 7:57:47 PM, 1"
"10/26/2017 7:57:49 PM, 1"
"10/26/2017 7:57:50 PM, 6"
"10/26/2017 7:57:52 PM, 5"

Upvotes: 1

Views: 135

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191701

If I change the csv file to have quotes around each row, everything works as expected

It shouldn't if you extracted the columns correctly. You only have one column if you put quotes around the whole row.

values = row[0].split(',')  # You are reading the first column, then splitting columns
dates.append(values[0])   # First part of the first column
timeToProcess(values[1])  # Second part of the first column

If you want to do it correctly, you can try

for row in reader:
    print row
    dates.append(row[0])
    timeToProcess(row[1])

Use a DictReader or pandas library if you want the headers to be able to extract the column values.

To rephrase your question Why do you need to add quotes to a csv File?, because columns can have commas themselves, and you should escape that column.

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

You're using csv.reader and manually splitting the rows. That's not necessary - the csv module's job is to isolate the "cells" for you (even if they contain metacharacters like commas or newlines). Just do

with open('test.csv', 'rb') as n:
    # ...  
    for row in reader:
        dates.append(row[0])
        timeToProcess(row[1]) # maybe better int(row[1])?

Also, please observe PEP-8 (Style Guide for Python code) - 4 spaces per indentation level.

Upvotes: 4

Related Questions