Skip to content

Instantly share code, notes, and snippets.

@alokm
Last active February 24, 2019 20:55
Show Gist options
  • Save alokm/2409a75365bc9a49fd26f9a0178346c1 to your computer and use it in GitHub Desktop.
Save alokm/2409a75365bc9a49fd26f9a0178346c1 to your computer and use it in GitHub Desktop.
CSVify.py converts a space delimited input file into a comma delimited output file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# CSVify converts a space delimited text file to csv and saves it
# with a .csv extension.
def csvify(infilename, outfilename):
with open(outfilename, 'w') as outfile:
with open(infilename, 'r') as infile:
for line in infile.readline():
# read, parse and write data one line at a time
raw_line = infile.readline()
csv_line = raw_line.replace(' ', ', ')
outfile.write(csv_line)
return "File conversion completed successfully!"
if __name__ == '__main__':
INPUT_FILE = 'data.dat' # full path to input file
OUTPUT_FILE = 'data.csv' # full path to output file
csvify(INPUT_FILE, OUTPUT_FILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment