使用 xlrd 模块读取 xlsx 文件并写入 csv 文件

Using xlrd module to read an xlsx file and write as a csv file

我正在使用 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_Media17726967388.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_Media17727585008.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_Media17734071276.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_Media17822966880.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_Media17807448979.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_Media17818069995.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_Media17809273947.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_Media17807655768.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_Media17812725009.jpg

Here is a link to the xlxs datafile

欢迎来到 SO!空行不是由数据引起的。您需要在打开名为 newline 的 csv 文件对象时添加一个参数。重写你的代码,

with open('refugeecamp.csv', 'w', newline='') as converted_csv:
    writer = csv.writer(converted_csv, quoting=csv.QUOTE_ALL)

有关详细信息,请参阅此 answer

希望对您有所帮助!干杯!