python 把EXCEL读取为dict
来源:互联网 发布:windows 2008ntp服务器 编辑:程序博客网 时间:2024/05/20 03:43
场景需求:
有一张excel表,表名为test.xlsx。格式如下
希望读成对应的json对象:
[{u’age’: 18.0, u’name’: u’john’}, {u’age’: 19.0, u’name’: u’hack’}, {u’age’: u”, u’name’: u’james’}]
#!/usr/bin/env ython# encoding:utf-8from xlrd import open_workbookfrom itertools import izip_longestdef main(): xlsfilename = 'test.xlsx' book = open_workbook(xlsfilename, formatting_info=False) cursheet = book.sheet_by_index(0) map_result = list() for row in range(cursheet.nrows): if row == 0: colum_list = [cursheet.cell(0, col).value for col in range(cursheet.ncols)] else: content = [cursheet.cell(row, col).value for col in range(cursheet.ncols)] content_map = map(lambda x: x.strip() if isinstance(x, basestring) else x, content) map_result.append(dict(izip_longest(colum_list, content_map))) print map_resultif __name__ == '__main__': main()
关键点:要把第一行的内容全部读取出来,作为key,把其他行的内容读取出来作为value
阅读全文
0 0
- python 把EXCEL读取为dict
- python把Excel数组转化为文本
- 他妈的 Python(2):把一个类转化为 dict 字典
- python读取xml放入dict字典中
- python把Teslink导出的xml格式转换为excel
- Python 把Excel文件导出为Json文件格式
- python把excel文件转换为指定的json格式
- python中将json字符串转换为dict
- python 读取excel内容为中文的处理
- python读取excel
- python 读取excel --zhaokuo719
- python 读取excel
- python读取EXCEL
- python读取excel
- python 读取Excel
- Python excel文件读取
- 用python读取excel
- Python 读取excel
- Mysql 存储引擎的区别和比较
- mnist的读取和图片转换
- QT学习杂项1
- EMV规范(四)——读应用数据
- 【剑指offer】题44:扑克牌的顺子
- python 把EXCEL读取为dict
- 汉字转拼音,用户表增加拼音字段,并将汉字姓名对应的拼音赋值给拼音字段
- BZOJ 3505 数三角形 (数论 组合数 gcd)
- 阿里巴巴CRM库问题
- 通信专业英语系列(一)
- Activity的启动模式详解
- 安卓s丢丢的简单使用
- GitLab教学支持系统Android练手项目
- Python的idle清屏