-5

for an assignment, I need to read past the first three lines of a csv file using python. I want all the information of the cvs file minus the first three lines, is there a way I can do this using python code? Thanks in advance for any help

def open_temperature_file(filename):
csv_file = open(filename, 'r')

reader = csv.reader(csv_file)
count = 0
for line in reader:
    count = count + 1
    if count == 3:
        break
return reader

Here is the code I've tried. The problem is that after reading after the first three lines, I have no way of saving the rest of the file (only the entire file)

questar10
  • 1
  • 1
  • 3
    Hi! Welcome to Stackoverflow. We are a community that likes to help each other but we will not do your homework for you. Show us some code that you've tried, show us the error you are getting and we will help correct the error, but you at least have to show us you are trying, first. – zenlc2000 Nov 08 '16 at 03:46
  • There is definitely a way to do this in Python, but as the comment above has clearly indicated, you need to show your own attempt at this and explain what difficulties you are having in your code. – idjaw Nov 08 '16 at 03:53
  • _ I have no way of saving the rest of the file (only the entire file)_ can you explain that better? What do you want to do with the csv reader after you've skipped 3 lines? Save it to another file? – tdelaney Nov 08 '16 at 06:14

2 Answers2

0

A couple of slight modifications will help:

def open_temperature_file(filename):
    full_text = []
    csv_file = open(filename, 'r')

    reader = csv.reader(csv_file)
    for line in reader:
        full_text.append(line)
    return full_text[3:] # return lines, starting from line 3

On the return above, I used a list slice to discard the first couple of lines.

zenlc2000
  • 451
  • 4
  • 9
0
import csv
skip_count = 3   # number of rows to be skipped, in your case its 3
with open('your_csv_file_path', 'r') as csvfile:
    for each in xrange(skip_count):
        next(csvfile)
    spamreader = csv.reader(csvfile)
    for eachrow in spamreader:
        print eachrow
Vishvajit Pathak
  • 3,351
  • 1
  • 21
  • 16