wind4869
6/17/2018 - 12:31 PM

xlrd

import sys
import xlrd

input_file, token, month, start, end = sys.argv[1:]
header = ['DATE', 'ABNORMAL', 'ANDROIDID&IMEI', 'IDFA', 'MZID', 'STABLE']

workbook = xlrd.open_workbook(input_file)
output_file = 'result.csv'
with open(output_file, 'w') as f:
    f.write('%s\n' % ','.join(header))

for day in xrange(int(start), int(end) + 1):
    date = month + '.' + str(day)
    sheet = workbook.sheet_by_name(date)
    result = { key: 0 for key in header[1:] }

    exists = False
    for i in xrange(5, sheet.nrows):
        if sheet.cell(i, 1).value == token:
            type_name = sheet.cell(i, 4).value
            result[type_name] += sheet.cell(i, 10).value
            if type_name == 'MZID':
                result['STABLE'] += sheet.cell(i, 12).value
            exists = True

    if exists:
        with open(output_file, 'a') as f:
            f.write(date)
            [f.write(',%.2f%%' % (result[key] * 100)) for key in header[1:]]
            f.write('\n')
    else:
        print('Token misses in date: %s' % date)