0

Code below takes an xls file and destination path. The code loops through all the worksheets in each file and converts the to a pipe delimited csv file.

def ExceltoCSV(excel_file, csv_file_base_path):
    head, tail = os.path.split(excel_file)
    print(tail)
    workbook = xlrd.open_workbook(excel_file)
    for sheet_name in workbook.sheet_names():
        print('processing - ' + sheet_name)
        worksheet = workbook.sheet_by_name(sheet_name)
        csv_file_full_path = csv_file_base_path + sheet_name.lower().replace(" - ", "_").replace(" ","_") + '.csv'
        csvfile = open(csv_file_full_path, 'w')
        writetocsv = csv.writer(csvfile, quoting = csv.QUOTE_ALL, delimiter='|')
        for rownum in range(worksheet.nrows):
             writetocsv.writerow(list((x.encode('utf-8')).decode() if type(x) == type(u'') else x for x in worksheet.row_values(rownum)))
        csvfile.close()
        print(sheet_name + ' has been saved at - ' + csv_file_full_path)

My issue with the function is that it creates the file with a \n after each line and it wraps each valve it writes inside "b'value'". I have tried the open with the wb option but I get an error: a bytes-like object is required, not 'str'

what should i change to avoid the extra lines and the wrapping of "b''"

Sample file output

"b''"|"b'Totals'"|"b'321'"|"b'734'"|"b'2,941,740'"|"b''"|"b''"|"b'Under - 11 Totals'"|"b'203'"|"b'77'"|"b''"|"b''"

"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"|"b''"
Leo Torres
  • 673
  • 1
  • 6
  • 18

0 Answers0