Search code examples
pythoncsvzippython-itertools

How to write lists into a csv using python


I am trying to write two lists into a csv file. I tried a couple of the solutions listed here: How to write data from two lists into columns in a csv? The izip solution did not work for me. When I tried it, I got this error:

ImportError: cannot import name 'izip'

I was able to import itertools, just not izip.

Then I tried the zip solution, but for some reason it ended up erasing the content of my original csv file. The original file is there, but now there's nothing in it. That's definitely not what I'm going for!

This is what I have right now:

for eas,nor in zip(xUTM, yUTM):
    cor=(utm.to_latlon(eas, nor, 20, 'Q'))
    treecor.append(cor)
l=list(map(list,zip(*treecor)))
lat=l[0]
lon=l[1]

I would like to create a duplicate of my original file and add a 'lat' column and a 'lon' column to the end of the duplicate (in that order). Is there an alternative to the solutions that I've tried so far?

Thank you!


Solution

  • I couldn't get any of the solutions I found to work. Finally a friend and I were able to come up with this:

    with open('cord_out.tsv','w')as fh:
        for i in treecor:
            print(i[0],'\t',i[1],file=fh)