I am trying to create a .csv file of UUID numbers. I see how to make a single UUID number in python but can't get the correct syntax to make 50 numbers and save them to a .csv file. I've googled and found many ways to create .csv files and how to use For loop but none seem to pertain to this particular application. Thank you for any help.
Asked
Active
Viewed 1,468 times
2 Answers
0
Just combine a csv writer with an uuid generator
import csv
import uuid
with open('uuids.csv', 'w') as csvfile:
uuidwriter = csv.writer(csvfile)
for i in range(50):
uuidwriter.writerow([uuid.uuid1()])

Nils Werner
- 34,832
- 7
- 76
- 98
-
Thank you for the quick reply. I never found uuidwriter when searching. I ran this and did get this error code: Traceback (most recent call last): File "C:/Users/bmiller/AppData/Local/Programs/Python/Python35/UUID Numbers_2.py", line 7, in
uuidwriter.writerow([uuid.uuid1()]) TypeError: a bytes-like object is required, not 'str'. ta – bmilleridentco Aug 03 '16 at 18:44 -
Hi Nils, I'm not sure what the error message means. I would like to learn what the correction is for it. Again, thank you for taking the time to answer my question ! – bmilleridentco Aug 03 '16 at 19:32
-
`uuidwriter` is not from the Python stdlib, it is an instance I am creating. I have fixed the code to not raise the `bytes-like object is required` error ([I accidentally opened the file as `wb` instead of `w`, where `b` stands for *bytemode*](http://stackoverflow.com/questions/13730107/writelines-writes-lines-without-newline-just-fills-the-file)). – Nils Werner Aug 04 '16 at 10:30
-
Hi again Nils, Thank you for taking the time to answer back. I do see now how uuidwriter is defined as the writer method. This code works perfectly for 50 numbers. Thanks again ! – bmilleridentco Aug 04 '16 at 13:50
-
I copied the ["short usage example" from the Python docs](https://docs.python.org/2/library/csv.html#csv.writer). But you are right, shorter is better in this case. – Nils Werner Aug 06 '16 at 14:21
-1
a csv is basically a text file, and since yours have only one column you won't need separators :
import uuid
with open('uuids.csv', 'w') as f:
f.writelines(str(uuid.uuid1()) + "\n" for i in range(50))

Loïc
- 11,804
- 1
- 31
- 49
-
Thanks Loïc, that works perfectly. I was trying before to put the "for i in range" statement in the beginning of my code and having it run like you have it or like Nils has it. Maybe that was my problem before. Thank you again for your help ! – bmilleridentco Aug 03 '16 at 18:47
-
You are not using `writelines` correctly. It expects a list of strings, each string being a line. Correct usage would be `f.writelines([str(uuid.uuid1()) for i in range(50)])`. Also your concept will not scale to millions or billions of lines as all content is saved to RAM before it is written to disk. – Nils Werner Aug 04 '16 at 10:32
-
@NilsWerner you might want to read this : https://www.python.org/dev/peps/pep-0289/ – Loïc Aug 05 '16 at 15:15
-
I know what generators are, I just misunderstood your code as a concatenation of strings. – Nils Werner Aug 06 '16 at 14:29