Skip to content

Instantly share code, notes, and snippets.

@u1735067
Last active March 14, 2018 17:47
Show Gist options
  • Save u1735067/3d5a3df58f074cc616498ce8a66069d8 to your computer and use it in GitHub Desktop.
Save u1735067/3d5a3df58f074cc616498ce8a66069d8 to your computer and use it in GitHub Desktop.
CSV Converter, from/to comma <> semicolon, for French users of Excel ; drag&drop file on the script. UTF-8 assumed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, pathlib, csv
def main():
if len(sys.argv) != 2:
print('Bad number of argument', file=sys.stderr)
sys.exit(1)
input_path = pathlib.Path(sys.argv[1])
if input_path.suffix != '.csv':
print('Input file does not have csv extension', file=sys.stderr)
sys.exit(2)
with input_path.open('r', encoding='utf8') as input_handle:
# Detect input delimiter
dialect = csv.Sniffer().sniff(input_handle.read(4096))
input_handle.seek(0)
input_reader = csv.reader(input_handle, dialect)
# Set output delimiter
if dialect.delimiter == ';': # Excel to not Excel
output_path = input_path.with_name(input_path.stem+'_comma'+input_path.suffix)
output_delimiter = ','
else: # Not Excel to Excel
output_path = input_path.with_name(input_path.stem+'_semicolon'+input_path.suffix)
output_delimiter = ';'
with output_path.open('x', encoding='utf-8-sig', newline='') as output_handle:
output_writer = csv.writer(output_handle, delimiter=output_delimiter)
for entry in input_reader:
output_writer.writerow(entry)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment