From 2d1b7eaecb1cc00e50fe689998ac4f107ad0a4fd Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sun, 10 Sep 2017 21:11:48 +0200 Subject: [PATCH] Update sahana-lang-csv.py to support both csv and xls. --- sahana/sahana-lang-csv.py | 48 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/sahana/sahana-lang-csv.py b/sahana/sahana-lang-csv.py index cb5d54b..1c46feb 100644 --- a/sahana/sahana-lang-csv.py +++ b/sahana/sahana-lang-csv.py @@ -1,26 +1,32 @@ #!/usr/bin/env python -import csv - -dictionary = {} -with open('cz.csv', 'rb') as csvfile: - reader = csv.reader(csvfile, delimiter=',', quotechar='\'') - for line in reader: - dictionary[line[1]] = line[2] - -with open('cz.py', 'wb') as langfile: - langfile.write(str(dictionary)) - --- - -import xlrd +from __future__ import print_function from pprint import pprint +import csv +import os +import sys +import xlrd -dictionary = {} -sheet = xlrd.open_workbook('cz.xls').sheet_by_index(0) -for i in range(1, sheet.nrows): - row = sheet.row(i) - dictionary[row[1].value.encode('utf-8')] = row[2].value.encode('utf-8') +if len(sys.argv) == 1: + print('Usage: {} input.(csv|xls)'.format(sys.argv[0])) + sys.exit(1) -with open('cz.py', 'wb') as langfile: - pprint(dictionary, langfile) +basename, extension = os.path.splitext(sys.argv[1].lower()) +translations = {} + +if extension == '.csv': + with open(sys.argv[0], 'rb') as csvfile: + reader = csv.reader(csvfile, delimiter=',', quotechar='\'') + for line in reader: + translations[line[1].encode('utf-8')] = line[2].encode('utf-8') +elif extension == '.xls': + sheet = xlrd.open_workbook(sys.argv[0]).sheet_by_index(0) + for i in range(1, sheet.nrows): + row = sheet.row(i) + translations[row[1].value.encode('utf-8')] = row[2].value.encode('utf-8') +else: + print('Unknown input file extension') + sys.exit(1) + +with open('{}.py'.format(basename), 'wb') as langfile: + pprint(translations, langfile)