Created
April 6, 2017 16:39
-
-
Save mattmc3/6eb61daf4d0eb04f3ca1332365cfe783 to your computer and use it in GitHub Desktop.
Python - csv to tsv
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 | |
| """ | |
| CSV to TSV | |
| """ | |
| import csv | |
| import re | |
| def csv2tsv(csvpath, delimiter=',', quotechar='"', newdelimiter='\t'): | |
| whitespacescrub = re.compile('\r|\n|\t') | |
| with open(csvpath) as csvfile: | |
| rdr = csv.reader(csvfile, delimiter=delimiter, quotechar=quotechar) | |
| for row in rdr: | |
| scrubbed_row = [whitespacescrub.sub(" ", x) for x in row] | |
| result = newdelimiter.join(scrubbed_row) | |
| print(result) | |
| if __name__ == '__main__': | |
| csv2tsv("test.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment