我正在使用xlrd
来读取xslx文件并将其以csv格式写入。当我在运行代码后打开csv文件时,每个填充的行后都有一个空行。我希望csv文件中的信息更加简洁,没有任何空行。
这是我的代码:
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()
前9行数据:
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
答案 0 :(得分:0)
欢迎您!空行不是由于数据引起的。打开名为newline
的csv文件对象时,需要添加一个参数。像这样重写代码,
with open('refugeecamp.csv', 'w', newline='') as converted_csv:
writer = csv.writer(converted_csv, quoting=csv.QUOTE_ALL)
请参阅此answer,以获取更多详细信息。
希望这会有所帮助!干杯!