0

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!

Jessica
  • 47
  • 1
  • 8
  • 3
    You're using a Python 2 answer in Python 3. In Python 3, `itertools.izip` was renamed to just plain old builtin `zip`. – abarnert Apr 24 '18 at 17:42
  • You are not showing your file handling code - and when you say lat log column - do you mean to each line? You will have to write out each line completely with the new columns. – AChampion Apr 24 '18 at 17:45
  • You're erasing the contents of the original file by opening the file name in write mode. If you're playing around with this kind of thing, I'd suggest different input and output file names until you have it working. – roganjosh Apr 24 '18 at 17:45
  • I've edited the Python 3 difference into the accepted answer on the question you linked to. Would the edited version have been enough to make the things clear to you? – abarnert Apr 24 '18 at 17:45
  • Aha! That explains that one! Would you be able to explain how to use zip? Or could you share a place I should look? – Jessica Apr 24 '18 at 17:46
  • You have a list of latitudes and a list of longitudes that exactly match the order of rows in the csv file? – tdelaney Apr 24 '18 at 17:50
  • The Tutorial section on [Looping Techniques](https://docs.python.org/3/tutorial/datastructures.html#looping-techniques) has a nice simple example of using `zip` (and Nested List Comprehensions elsewhere in the same chapter has another example of the `zip(*something)` transposing idiom, but I'd make sure I understand the first one, and what `*` splatting is, before reading that part). If that isn't enough, there are better tutorials (and books and courses) out there than the official one, but I don't really know them. – abarnert Apr 24 '18 at 17:52
  • Thanks for your help @abarnet I'm still having a bit of difficulty, but I think the tutorial will help a lot! – Jessica Apr 24 '18 at 17:53
  • @tdelaney It should match most of the time. I'm sure there will be some cases where it won't though. I'm not sure how to handle that (maybe a dictionary?), but I'm not worried about that problem at the moment. – Jessica Apr 24 '18 at 17:57

2 Answers2

0

You could write treecor using zip and a generator to apply the mapping function. This is usually preferred over the map function.

treecor = [utm.to_latlon(eas, nor, 20, 'Q') for eas, nor in zip(xUTM, yUTM)]

Or skip that step completely and only calculate lat/lon as you need it. Since you are appending columns you need to overwrite the csv file completely. And that's best done to a temporary file that you rename on success. Use zip to combine rows from the csv with items in the utm lists:

import os

xUTM, yUTM = get_utm_lists()

filename = 'orig.csv'

try:
    with open(filename) as in_fp, open(filename + ".tmp", "w") as out_fp:
        in_csv = csv.reader(in_fp)
        out_csv = csv.writer(out_fp)
        for row, eas, nor in zip(in_csv, xUTM, yUTM):
            out_csv.write(row + *utm.to_latlon(eas, nor, 20, 'Q'))
    os.rename(filename + ".tmp", filename)
except OSError as e:
    print(e)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

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)
Jessica
  • 47
  • 1
  • 8