Reputation: 159
I am using xlrd
to read an xslx file and write it in csv format. When I open the csv file after running the code, there is an empty row after each filled one. I would like the information in the csv file to be a bit more condensed without any empty rows.
Here is my code:
import xlrd
import csv
def csv_from_excel():
workbook = xlrd.open_workbook('refugee_camp.xlsx')
sheet = workbook.sheet_by_index(0)
converted_csv = open('refugeecamp.csv', 'w')
wr = csv.writer(converted_csv, quoting=csv.QUOTE_ALL)
for rownum in range(sheet.nrows):
wr.writerow(sheet.row_values(rownum))
converted_csv.close()
try:
open_file = open("refugeecamp.csv", 'r')
print("Yesss")
except:
print("could not open file!")
csv_from_excel()
The first 9 rows of data:
0 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MQ_1 MQ Mosque 21.21224574 92.16796803 priv_don n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517726967388.jpg
1 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_CF_1 CF Child Friendly Space 21.21258107 92.16746224 ngo_un Unicef No Yes No Yes G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517727585008.jpg
2 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MD_1 MD Madrassah 21.21039682 92.16750261 priv_don n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517734071276.jpg
3 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MD_2 MD Madrassah 21.20590394 92.15938967 local_gov n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517822966880.jpg
4 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_CF_2 CF Child Friendly Space 21.2059306 92.16004456 ngo_un Mukti Ukaid Unicef No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517807448979.jpg
5 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MQ_2 MQ Mosque 21.20411076 92.16006021 local_gov n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517818069995.jpg
6 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MQ_3 MQ Mosque 21.207251 92.16507649 priv_don n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517809273947.jpg
7 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MQ_4 MQ Mosque 21.2062252 92.1641497 priv_don n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517807655768.jpg
8 Coxs_Bazar Ukhia Palong Khali Spontaneous Site Camp 2E CXB-203 C2E_MQ_5 MQ Mosque 21.20711018 92.16626172 priv_don n/a No No No No G:\ROUND 4 Infrastructure\Round4_Infrastructure_Media\1517812725009.jpg
Here is a link to the xlxs datafile
Upvotes: 0
Views: 1222
Reputation: 1402
Welcome to SO! The empty rows are caused not because of data. You need to add a parameter while opening the csv file object called newline
. Rewrite your code like,
with open('refugeecamp.csv', 'w', newline='') as converted_csv:
writer = csv.writer(converted_csv, quoting=csv.QUOTE_ALL)
Refer this answer for more details.
Hope this helps! Cheers!
Upvotes: 1