python 展平一系列Excel列,这些列在单元格中包含列表,同时保留行。允许为不可打印的U设置错误级别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 展平一系列Excel列,这些列在单元格中包含列表,同时保留行。允许为不可打印的U设置错误级别相关的知识,希望对你有一定的参考价值。
import csv
import sys
from codecs import encode
from itertools import imap
from pyexcelerate import Workbook
from toolz.curried import *
from xlrd import open_workbook
BOOK = 'cv_fam.xlsx'
SHEET = 'FINAL - Events List'
COLUMN_NAMES = ('CV', 'PR', 'FL', 'CR')
START_ROW = 2
STOP_ROW = None
START_COL = 2
STOP_COL = 6
def xlsreader(workbook, worksheet):
book = open_workbook('cv_fam.xlsx')
sheet = book.sheet_by_name('FINAL- Events List')
for row_num in range(sheet.nrows):
yield sheet.row(row_num)
def cell2str(cell, errors='replace'):
try:
return str(cell.value)
except UnicodeEncodeError:
return encode(cell.value, 'utf-8', errors)
def flatten(rows):
#import pdb; pdb.set_trace()
for row_num, row in enumerate(rows):
columns = row[START_COL:STOP_COL]
for col_num, col in enumerate(columns):
col_name = COLUMN_NAMES[col_num]
for item in col.split():
columns = (row[:START_COL], [col_name, item], row[STOP_COL:])
yield concat(columns)
def main():
import pdb; pdb.set_trace()
rows = list(xlsreader(BOOK, SHEET))[START_ROW:STOP_ROW]
rows = [list(map(cell2str, i)) for i in rows]
rows = list(flatten(rows))
book = Workbook()
book.new_sheet(SHEET, data=rows)
book.save(BOOK + '-flat.xlsx')
if __name__ == '__main__':
main()
以上是关于python 展平一系列Excel列,这些列在单元格中包含列表,同时保留行。允许为不可打印的U设置错误级别的主要内容,如果未能解决你的问题,请参考以下文章
Pandas Dataframe 根据列值将值展平到单元格
求助excel单元格不同自动添加3个空白行
excel每个单元格多行内加入相同的内容?
excel的数值显示
Excel中几行几列怎么求和呢?
excel怎么对一列进行求和