Skip to content

Instantly share code, notes, and snippets.

@freejoe76
Created November 25, 2014 16:52
Show Gist options
  • Save freejoe76/8b9ba59e0e4918346ce9 to your computer and use it in GitHub Desktop.
Save freejoe76/8b9ba59e0e4918346ce9 to your computer and use it in GitHub Desktop.
Parse a CSV into another file
#!/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