usr bin env python import sys import xlwt import csv import six def no

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
import sys
import xlwt
import csv
import six
def normalize_value(val):
if six.PY2:
if isinstance(val, str):
val = val.decode('utf-8')
return val
def main():
path = sys.argv[1]
if not path.endswith('.csv'):
logging.error('Input file name does not ends with .csv')
sys.exit(1)
else:
reader = csv.reader(open(path))
wb = xlwt.Workbook()
ws = wb.add_sheet('data')
count = 0
for row_idx, row in enumerate(reader):
for col_idx, val in enumerate(row):
ws.write(row_idx, col_idx, normalize_value(val))
count += 1
new_path = path[:-4] + '.xls'
wb.save(new_path)
print('Converted %d rows' % count)
if __name__ == '__main__':
main()