Python实现将json文件中向量写入Excel的方法


Posted in Python onMarch 26, 2018

本文实例讲述了Python实现将json文件中向量写入Excel的方法。分享给大家供大家参考,具体如下:

有一个需要是将已经向量化的页面的数据中的向量写入Excel中用于展示,工作很简单,这里简单复习一下Excel文件写的工作,试验中使用的json文件内容如下:

[

{"vector": [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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水寒城
功能:python实现json文件中向量写入Excel中
'''
from xlwt import *
import json
def write2excel(vectorfile='vector.json'):
  '''''
  将向量写入Excel中
  '''
  with open(vectorfile) as f:
    vector=json.load(f)
  i=0
  book=Workbook(encoding = 'utf-8')
  table=book.add_sheet('vector')
  for one_dict in vector:
    name=one_dict['name']
    content=one_dict['vector'][0:200]
    for j in range(len(content)):
      table.write(i,j,content[j])
    i+=1
  book.save('vector.xls')
if __name__ == '__main__':
  write2excel(vectorfile='vector.json')

这里有一个问题就是,列索引必须在256以内,而实验使用的数据是400多的,为了简单起见只是截取了前200列来实验了

这里只是简单实现了写入Excel文件的功能,还有其他的方法就不多说了。

Python 相关文章推荐
Python读取图片EXIF信息类库介绍和使用实例
Jul 10 Python
Python中AND、OR的一个使用小技巧
Feb 18 Python
python基础教程之匿名函数lambda
Jan 17 Python
Python实现识别手写数字大纲
Jan 29 Python
查找python项目依赖并生成requirements.txt的方法
Jul 10 Python
详解Numpy中的数组拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)
May 27 Python
python实现桌面气泡提示功能
Jul 29 Python
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
Aug 19 Python
Python爬虫实现“盗取”微信好友信息的方法分析
Sep 16 Python
python打印异常信息的两种实现方式
Dec 24 Python
python3实现将json对象存入Redis以及数据的导入导出
Jul 16 Python
python打包生成so文件的实现
Oct 30 Python
Python元组拆包和具名元组解析实例详解
Mar 26 #Python
基于循环神经网络(RNN)实现影评情感分类
Mar 26 #Python
基于循环神经网络(RNN)的古诗生成器
Mar 26 #Python
python机器学习之随机森林(七)
Mar 26 #Python
Python实现扣除个人税后的工资计算器示例
Mar 26 #Python
python实现决策树、随机森林的简单原理
Mar 26 #Python
python机器学习之贝叶斯分类
Mar 26 #Python
You might like
PHP编码规范-php coding standard
2007/03/16 PHP
PHP加密解密类实例代码
2016/07/20 PHP
PHP两个n位的二进制整数相加问题的解决
2018/08/26 PHP
php版本CKEditor 4和CKFinder安装及配置方法图文教程
2019/06/05 PHP
从数据结构的角度分析 for each in 比 for in 快的多
2013/07/07 Javascript
原生JS实现表单checkbook获取已选择的值
2013/07/21 Javascript
escape编码与unescape解码汉字出现乱码的解决方法
2014/07/02 Javascript
轻松学习jQuery插件EasyUI EasyUI实现拖放商品放置购物车
2015/11/30 Javascript
jQuery的ajax下载blob文件
2016/07/21 Javascript
滚动条的监听与内容随着滚动条动态加载的实现
2017/02/08 Javascript
Javascript实现从小到大的数组转换成二叉搜索树
2017/06/13 Javascript
React之PureComponent的使用作用
2018/07/10 Javascript
详解mpvue中小程序自定义导航组件开发指南
2019/02/11 Javascript
vscode vue 文件模板的配置方法
2019/07/23 Javascript
OpenLayer3自定义测量控件MeasureTool
2020/09/28 Javascript
python数组复制拷贝的实现方法
2015/06/09 Python
Python3学习笔记之列表方法示例详解
2017/10/06 Python
对Python中Iterator和Iterable的区别详解
2018/10/18 Python
详解python的argpare和click模块小结
2019/03/31 Python
python数据库编程 Mysql实现通讯录
2020/03/27 Python
一文读懂python Scrapy爬虫框架
2021/02/24 Python
关于canvas绘制模糊问题的解决方法
2019/09/24 HTML / CSS
Java语言的优势
2015/01/10 面试题
涉外文秘个人求职的自我评价
2013/10/07 职场文书
就业推荐表自我鉴定
2013/10/29 职场文书
城市精细化管理实施方案
2014/03/04 职场文书
师恩难忘教学反思
2014/04/27 职场文书
环保小标语
2014/06/13 职场文书
校园文化艺术节宣传标语
2014/10/09 职场文书
党的群众路线教育实践活动调研报告
2014/11/03 职场文书
仓库管理制度范本
2015/08/04 职场文书
小学一年级班主任工作经验交流材料
2015/11/02 职场文书
2016年机关单位节能宣传周活动总结
2016/04/05 职场文书
Python关于OS文件目录处理的实例分享
2021/05/23 Python
浅谈MySQL表空间回收的正确姿势
2021/10/05 MySQL
Oracle锁表解决方法的详细记录
2022/06/05 Oracle