简体   繁体   English

使用xlrd模块读取xlsx文件并作为CSV文件写入

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

I am using xlrd to read an xslx file and write it in csv format. 我正在使用xlrd读取xslx文件并将其以csv格式写入。 When I open the csv file after running the code, there is an empty row after each filled one. 当我在运行代码后打开csv文件时,每个填充的行后都有一个空行。 I would like the information in the csv file to be a bit more condensed without any empty rows. 我希望csv文件中的信息更加简洁,没有任何空行。

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: 前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

Here is a link to the xlxs datafile 这是指向xlxs数据文件的链接

Welcome to SO! 欢迎来到SO! The empty rows are caused not because of data. 空行不是由于数据引起的。 You need to add a parameter while opening the csv file object called newline . 您需要在打开名为newline的csv文件对象时添加一个参数。 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! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM