1

I am trying to import csv file to Python. I have two question here.

Q1. my code is:

import csv
with open('highfrequency2.csv') as csvfile:
  freq=csv.reader(csvfile, delimiter=',')
  for row in freq:
    print(row[1],row[8])

But there is an error message here

IndexError: list index out of range

Q2. my data output looks like

['WOSl.TQ', '02-Jan-14', '51:48.0', 'Quote', '', '']
['WOSl.TQ', '02-Jan-14', '51:48.0', 'Quote', '', '']
['WOSl.TQ', '02-Jan-14', '51:48.0', 'Quote', '', '']
['WOSl.TQ', '02-Jan-14', '51:48.0', 'Quote', '', ''] 
['WOSl.TQ', '02-Jan-14', '51:48.0', 'Quote', '', '']
.....
....

Do I have to use 'r' or 'rb' function in reading the file ?

I want to see the 2nd and the 8th row of the CSV file.

1
  • The first thing I would do if it was me doing this problem is to run csvlint to make sure the csv file is correct. Commented Apr 17, 2015 at 10:28

2 Answers 2

2

your row has 6 elements and you're trying to print the 2nd and the 9th. well, the latter does not exist, hence list index out of range (indexing starts with 0).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks very much. I think i made a stupid mistake. In excel, Row means horizontal line, but in Python Row means vertical line. Well, I just want to see the first several horizontal lines of csv data. Anything I could do about that ? Thanks again.
i believe freq is a list of lists? just go for i in range(8): print freq[i]?
@ Padraic Cunningham, I see, I didn't verify my assumption.
1

If you just want rows 2-8, use itertools.islice on the reader object:

import csv
from itertools import islice, izip

with open('highfrequency2.csv') as csvfile:
    freq = csv.reader(csvfile)
    # create islice object
    interest = islice(freq, 1, 8, None))
    # iterate over islice object getting row at a time
    for row in interest:
       print(row)

If you just want those two specific rows use a step of 6:

 import csv
from itertools import islice, izip

with open('highfrequency2.csv') as csvfile:
    freq = csv.reader(csvfile)
    interest = islice(freq, 1, 8, 6))
    for row in interest:
       print(row)

Or call next on the reader object after skipping the first line:

import csv

with open('highfrequency2.csv') as csvfile:
    freq = csv.reader(csvfile)
    next(freq)
    # create generator expression
    interest = (next(freq) for _ in range(7))
    for row in interest:
       print(row)

Or using enumerate:

 import csv

with open('highfrequency2.csv') as csvfile:
    freq = csv.reader(csvfile)
    interest = (row for ind, row in enumerate(freq) if ind in {1,7})
    for row in interest:
       print(row)

8 Comments

Thank you very much indeed Padraic. Just another quick one.
@TristanSun, sure, what is the issue?
Thank you very much indeed Padraic. It works. Just another quick one. How can I generate the output in table format ? Just like my original table format. Thanks !
Do you mean to a csv file?
I used your code and the output came all together. I just want to see the output line by line. Is there any way to do this ? Like my original table. Thanks!
|

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.