python excel到csv转换生成错误的输出

时间:2018-04-24 17:40:38

标签: python excel csv

我在下面编写了此代码,我将excel工作表转换为csv文件。如果每个excel单元格中有单行数据,但如果excel单元格有多个行数据,那么它会产生错误的输出。以下是excel的示例数据:

Domain ; State ; Industry; Simulation ; Simulation offers public quick access to  information and analysis – e.g., reports, news, data, and trends. 
Domain ; State ; Industry ; VT ; VR enables people to view simulation of virtual environement
Dynamic visualization increases analysis capabilities
Provides answers to questions such as:
- Where are the biggest opportunities for scaling?
- How should resources be aligned to increase performance?
- How much are people are spending per transaction or on a particular virtualization?

正如您所看到的,excel工作表的第一行在每个单元格中都有单行数据,因此它是正确的,但第二行在其最后一个单元格中有多行数据,当我运行我的代码,它将此单元格中的每一行视为单独的值,因此每行都在转换后的csv中的单独行中。以下是我的代码:

import xlrd
import csv


def Excel2CSV(ExcelFile, SheetName, CSVFile):

    workbook = xlrd.open_workbook(ExcelFile)
    worksheet = workbook.sheet_by_name(SheetName)
    csvfile = open(CSVFile, 'w',newline = '',encoding='utf8')
    wr = csv.writer(csvfile,delimiter=';')

    for rownum in range(worksheet.nrows):

        wr.writerow(worksheet.row_values(rownum))

    csvfile.close()

在将excel工作表转换为csv时,如何确保单个单元格中的多个行数据保持在一起(我不关心来自Excel工作单的多行数据是否为单个) csv中的行数据?

0 个答案:

没有答案