Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading .csv in Python without looping through the whole file?

The only way I've seen Python's csv.reader used is in a for loop, which goes through the whole file without saving past values of the read in variables. I only need to work with 2 consecutive lines of the (enormous) file at a time. Using the csv.reader for loop, I only have 1 line at a time.

Is there a way to use Python's csv module for taking in only one line of a csv file without having to finish reading the file to the end?

I need to set variables to the values in the first line, set a second set of variables to the values of the next line, use the two sets of variables simultaneously for computations, then overwrite the first set of variables with the second set, and read a new line to overwrite the second set.

like image 214
mary Avatar asked Jun 23 '10 16:06

mary


People also ask

Does csv reader read the whole file?

It is only a pointer it will not load the whole file in the memory. However, it actually depends on the way you are opening the file. .read() loads the complete file in the ram.

How do I read a csv file without the header in Python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.

How do you read and write a csv file simultaneously in Python?

csv' with open(in_path, 'r', newline='') as inputFile, open(out_path, 'w', newline='') as writerFile: read_file = csv. reader(inputFile) write_file = csv. writer(writerFile, delimiter=' ', quotechar='|', quoting=csv. QUOTE_MINIMAL) for row in read_file: # your modifying input data code here ........


3 Answers

There's nothing forcing you to use the reader in a loop. Just read the first line, then read the second line.

import csv
rdr = csv.reader(open("data.csv"))
line1 = rdr.next() # in Python 2, or next(rdr) in Python 3
line2 = rdr.next()
like image 190
Bryan Oakley Avatar answered Oct 27 '22 01:10

Bryan Oakley


If you're always looking at exactly two consecutive lines, it sounds to me like you might benefit from using the pairwise recipe. From the itertools module:

from itertools import tee, izip
def pairwise(iterable):
   "s -> (s0,s1), (s1,s2), (s2, s3), ..."
   a, b = tee(iterable)
   next(b, None)
   return izip(a, b)

You would use this like so:

for first_dict, second_dict in pairwise(csv.DictReader(stream)):
    # do stuff with first_dict and second_dict
like image 33
Jeffrey Harris Avatar answered Oct 27 '22 02:10

Jeffrey Harris


Read CSV:

readCSV = csv.reader(csvFile, delimiter=',')

Read the next row in Python 2.7:

    row = readCSV.next()

Read the next row in Python 3.4:

    row = readCSV.__next__()
like image 5
hatef Avatar answered Oct 27 '22 02:10

hatef