我在下面编写了此代码,我将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中的行数据?