Search code examples
pythonexcelcsvxlsxwriter

xlsxwriter creates empty rows between full rows


i wrote a python code to generate an excel file from a csv file using xlsxwriter. but the output is not what i wanted. i got empty rows between my data:

enter image description here

i don't know what make this happen in my code. i want to remove those empty lines like this:

enter image description here

for csvfile in glob.glob(os.path.join('.', '*.csv')):
    workbook = Workbook(csvfile[:-4] + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rt', encoding='cp1252') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)
    workbook.close()

i appreciate your help.


Solution

  • Try

    with open('sample.csv', 'a', newline='') as f:

    The examples in the docs use newline=''.