0

I am using Python 2.6 and I want to write in a csv file. This is my code.

fieldnames = ['Date', 'AICENT MO', 'AICENT MT', 'BELGACOM MO', 'BELGACOM MT', 'CITIC MO', 'CITIC MT', 'SYBASE MO','SYBASE MT']
csvfile = open('%s%s_%s.csv'%(report,module_name,lastMonth), 'w')
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(fieldnames)
list = [date_file,col1,col2,col3,col4,col5,col6,col7,col8]  
writer.writerow(list)   

My Output is:

enter image description here

How to remove those empty rows?

Lelan Calusay
  • 51
  • 2
  • 9

1 Answers1

0

This question is a duplicate of Writing to CSV with Python adds blank lines

The way you are opening the file is wrong. You need to change your line open('%s%s_%s.csv'%(report,module_name,lastMonth), 'w') to open('%s%s_%s.csv'%(report,module_name,lastMonth), 'wb'). That is you should open the file as a binary file.

Community
  • 1
  • 1
Vaulstein
  • 15,782
  • 6
  • 40
  • 57
  • Hi @Vaulstein, I already changed to open('%s%s_%s.csv'%(report,module_name,lastMonth), 'wb') but the output is still the same. – Lelan Calusay Apr 21 '15 at 06:10
  • Then the problem is with the list you are trying to write to the file. I tried having the data in the format of a list within a list and the output file did not have any extra empty rows. I would say you do something like: hello = [['Me','You'],['293', '219'],['13','15']] length = len(hello[0]) And then while writing do: for y in range(length): csv_writer.writerow([x[y] for x in hello]) – Vaulstein Apr 21 '15 at 06:44