CSV reader and unique ids

Tim Golden mail at timgolden.me.uk
Mon Sep 1 12:09:49 EDT 2008


Mike P wrote:
> I'm trying to use the CSV module to read in some data and then use a
> hashable method (as there are millions of records) to find unique ids
> and push these out to another file,

You could either zip with a counter or use the uuid module,
depending on just how unique you want your ids to be.

<code>
import os, sys
import csv
import itertools
import uuid

stuff = "the quick brown fox jumps over the lazy dog".split ()

f = open ("output.csv", "wb")
writer = csv.writer (f)

#
# Style 1 - numeric counter
#
writer.writerows (zip (itertools.count (), stuff))

#
# Style 2 - uuid
#
writer.writerows ((uuid.uuid1 (), s) for s in stuff)

f.close ()
os.startfile ("output.csv")

</code>

TJG



More information about the Python-list mailing list