Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- with open("my_file.csv", "w") as f: # with is a context manager, as long as you are in this block, f is an open file
- writer = csv.writer(f) # wraps our file (f) in a csv writer. This handles writing to CSV for us.
- for r in range(5): # This is just to generate some fake data
- # writer.writerow takes a list of values to write as one row in the file. The csv writer handles
- # formatting the file for you.
- writer.writerow((r, r+1, r+2, r+3))
- # Will create a file called "my_file.csv" with contents
- # 0, 1, 2, 3
- # 1, 2, 3, 4
- # 2, 3, 4, 5
- # 3, 4, 5, 6
- # 4, 5, 6, 7
Add Comment
Please, Sign In to add comment