Created
November 25, 2014 16:52
-
-
Save freejoe76/8b9ba59e0e4918346ce9 to your computer and use it in GitHub Desktop.
Parse a CSV into another file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
def main(): | |
""" Take a CSV, write it to a file. """ | |
with open('name-of-file.csv', 'rb') as csvfile: | |
i = 0 | |
content = [] | |
reader = csv.reader(csvfile) | |
for row in reader: | |
if i == 0: | |
keys = row | |
continue | |
record = dict(zip(keys,row)) | |
content += ['this is the line you will write to the new file. %(fieldname)s is how you include a variable. The "s" stands for "string"' % record] | |
file_handler = open('output.json', 'w') | |
for item in content: | |
file_handler.write("%s\n" % item) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment