Last active
August 19, 2017 12:04
-
-
Save tobywf/bf48dd4210186ec8cf826f79a6ca66fa to your computer and use it in GitHub Desktop.
Generate CSV files with all permutations of common line endings
This file contains 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
# -*- coding: utf-8 -*- | |
from __future__ import print_function, unicode_literals | |
from itertools import product | |
import sys | |
try: | |
encoding = sys.argv[1] | |
except IndexError: | |
encoding = 'utf-8' | |
newlines = { | |
'\n': 'LF', | |
'\r': 'CR', | |
'\r\n': 'CR+LF', | |
} | |
for outer, inner in product(newlines.keys(), repeat=2): | |
description = inner.join([ | |
'* This', | |
'* Is', | |
'* An', | |
'* Example', | |
]) | |
data = outer.join([ | |
'id,sku,description', | |
'100,AAAA,"{}"'.format(description), | |
'101,AAAB,🎅', | |
'102,AAAC,EOF', | |
]) | |
# write binary to avoid any newline translation | |
with open('{}_{}.csv'.format(newlines[outer], newlines[inner]), 'wb') as fp: | |
fp.write(data.encode(encoding)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment