1

I have two files that I am going to use for plotting. one contains the x,y range and another contains data.

The following is my code to get data from one file using CSV reader.

(I have data in x,y \n format in each line of file in range.dat)

import csv
with open('range.dat', 'r') as f,open('data,dat','r') as d:
         reader = csv.reader(f)
         reader1 = csv.reader(d)
         for row in reader:
             print row
             x, y = map(float, row[0:2])
             print [x,y]

Now the problem is I also want data from data.dat at the same time from the same row and want to append it to [x,y] such that final list will be [x,y,data]. Is there any method through which i can iterate through data.dat content in same for loop.

0

1 Answer 1

4

The best would probably be to zip those two readers together. If you are using Python 2, you should use itertools.izip to get a generator; for Python 3 you can just use zip.

for row, data in itertools.izip(reader, reader1):
    x, y = map(float, row[0:2])
    print [x,y, data]

Alternatively, you could also call next on the other reader:

for row in reader:
    data = next(reader1)
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

@Hima You're welcome. Note that when you use just zip on Python 2.x, this will read in both files at once and store their contents in a list. If the files are very big, you should rather use izip, which will only keep one line from each file in memory.
I am using python 2.7 but in future I am going to deal with large files containing millions of such data , so itertools.izip will make difference for me.. its better to use it from now . what do you mean by "reading files at once " in case of zip, it just reads line by line and will return the list while itertools.izip returns the iterator right ?
@Hima Try it. Remove the for loop and instead do print zip(...), or print type(zip(...)). zip in Python 2 will immediately read all the files and stores the content in one big list of tuples, while both izip and zip in Python 3 create a generator.

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.