我试图拆分excel电子表格单元格的内容

时间:2018-05-23 20:29:49

标签: python excel

我正在尝试从excel电子表格中拆分单元格的内容,不熟悉Python,我无法弄清楚它的语法。我正在使用xlrdxlwt库。到目前为止,我可以获得我需要的正确列,并希望将单元格pvalue的内容拆分为类似于excel函数text to columns,这在python中是否可行?我能在网上找到的所有东西,已经有一个整齐格式化的数据与所有数据点在一个单独的列中,我想知道我是否能在正确的方向上得到一个点。这是我到目前为止的代码

import xlrd
import xlwt

# open and create workbook
workbook = xlrd.open_workbook('myexcelfile.xlsx')
workbookwrite = xlwt.Workbook('output.xlsx')

# open and create sheets
worksheet = workbook.sheet_by_index(0)
sheet = workbookwrite.add_sheet('Sheet1')

i = 1

# iterate through rows in worksheet, get columns completed_at and name
for rows in range(worksheet.nrows):
   cell1 = worksheet.cell(rows, 3).value
   cell2 = worksheet.cell(rows, 15).value
   row = sheet.row(i)

# if a completed_at date is empty, don't write data to new workbook
    if cell1 != '':
       value = cell2
       pvalue = cell2.split(',')
       row.write(0, cell1)
       row.write(1, pvalue)
   i += 1

workbookwrite.save('output.xls')

如果我可以在不同的库上使用,如果有一个更好的那个将是伟大的。感谢

1 个答案:

答案 0 :(得分:0)

pvaluecell2中以逗号分隔的项目列表,您希望将pvalue的每个元素写入从第1列开始的自己的列中。 ,只需执行这样的for循环:

for col, element in enumerate(pvalue):
    row.write(col+1, element)