Python完成将json文件中向量写入Excel的章程

<!-------------知识的力量是最最的(当然料定还应该有更不难的方式)-----------!>

正文实例陈诉了Python达成将json文件中向量写入Excel的点子。分享给我们供我们参谋,具体如下:

 

有一个急需是将已经向量化的页面包车型大巴多寡中的向量写入Excel中用来展现,职业异常的粗略,这里大致复习一下Excel文件写的专门的学问,试验中应用的json文件内容如下:

当自个儿着想将省市区三级联合浮动数据从mysql转入mongodb时相遇了网络无间接插入mongodb的示范(基本均是mysql插入示例)。于是想到利用json文件一向导入mongodb会相比easy(SQLyog怎么样导出json?)

复制代码 代码如下:

在SQLyog中写七个json格式查询语句:(省市区example如下:)看一下就能够懂此中法则*

[
{"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,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0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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], "name": "12"}
]

SELECT
'{"code":"' AS a,
cities.`cityid` AS b,
'","name":"' AS c,
cities.`city` AS d,
'","provinceCode":"' AS e,
cities.`provinceid` AS f,
'"}' AS g
FROM cities

代码如下:

 图片 1图片 2

#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能: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列来试验了

图片 3

此间只是简短完毕了写入Excel文件的机能,还可能有其余的格局就异常少说了。

图片 4

PS:关于json操作,这里再为我们推荐两款相比实用的json在线工具供我们仿效运用:

接下去是将导出的json拼接成三个json文件:(在notpad 中以^查找来替换,拼接jsonarray串加array名(记得举办json校验与json压缩管理))

在线JSON代码查证、查验、美化、格式化工具:

json在线校验UOdysseyL: 

JSON**在线格式化学工业具:
**

json在线压缩U福特ExplorerL:

在线XML/JSON相互调换工具:

图片 5图片 6

json**代码在线格式化/美化/压缩/编辑/转变工具:
**

最终以压缩成八个json文件的花样在java代码中剖判

在线json压缩/转志愿者具:

图片 7

更加的多Python相关内容感兴趣的读者可查看本站专项论题:《Python操作Excel表格本事计算》、《Python操作json本领总计》、《Python编码操作才具总计》、《Python数据结构与算法教程》、《Python函数使用技艺计算》、《Python字符串操作才能汇总》、《Python入门与进级出色教程》及《Python文件与目录操作技艺汇总》

着力优良:

期望本文所述对大家Python程序设计有着帮忙。

 

你大概感兴趣的稿子:

  • Python完结把json格式转变到文本或sql文件
  • Python3兑现将地点JSON大数据文件写入MySQL数据库的章程
  • python查询mysql,返回json的实例
  • Python管理JSON数据并生成条形图
  • python读写json文件的概括达成
  • python读取json文件并将数据插入到mongodb的点子
  • python 读写txt文件 json文件的实现格局
  • python中分析json格式文件的措施身体力行
  • Python完毕读取json文件到excel表
  • Python实现的读写json文件功用示例
  • Python SQL查询并生成json文件操作示例
 1 @Test
 2 public void testProCityArea(){
 3 String fileName = "ProvCityArea.geojson";
 4 String path = System.getProperty("user.dir")   "\src\main\webapp\static\geojson\"   fileName;
 5 JSONObject jsonobject = JSONObject.parseObject(FileHelper.readFile(path));
 6 JSONArray provArray = jsonobject.getJSONArray("provinces"); 
 7 for (Object object : provArray) {
 8 JSONObject provJson = (JSONObject) object;
 9 Province province = new Province(GuidUtils.getInstance().getGuid(), provJson.getString("code"), provJson.getString("name"));
10 mongoTemplate.insert(province, "province");
11 }
12 JSONArray cityArray = jsonobject.getJSONArray("city");
13 for (Object object : cityArray) {
14 JSONObject cityJson = (JSONObject) object;
15 City city = new City(GuidUtils.getInstance().getGuid(), cityJson.getString("code"), cityJson.getString("name"), cityJson.getString("provinceCode"));
16 mongoTemplate.insert(city, "city");
17 }
18 JSONArray areaArray = jsonobject.getJSONArray("area");
19 for (Object object : areaArray) {
20 JSONObject areaJson = (JSONObject) object;
21 Area area = new Area(GuidUtils.getInstance().getGuid(), areaJson.getString("code"), areaJson.getString("name"), areaJson.getString("cityCode"));
22 mongoTemplate.insert(area, "area");
23 }
24 }

FileHelper:

 

 1 import java.io.BufferedReader;
 2 import java.io.FileInputStream;
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 
 6 public class FileHelper {
 7 
 8 public static String readFile(String path){
 9 BufferedReader reader = null;
10 String laststr = "";
11 try{
12 FileInputStream fileInputStream = new FileInputStream(path);
13 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
14 reader = new BufferedReader(inputStreamReader);
15 String tempString = null;
16 while((tempString = reader.readLine()) != null){
17 laststr  = tempString;
18 }
19 reader.close();
20 }catch(IOException e){
21 e.printStackTrace();
22 }finally{
23 if(reader != null){
24 try {
25 reader.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 }
29 }
30 }
31 return laststr;    
32 }
33 
34 }

 

GuidUtils

 1 public class GuidUtils {
 2 
 3 private static final GuidUtils instance = new GuidUtils();
 4 
 5 private GuidUtils(){
 6 
 7 }
 8 
 9 public static GuidUtils getInstance() {
10 return instance;
11 }
12 
13 public String getGuid() {
14 UUID uuid = UUID.randomUUID();
15 String guid = uuid.toString();
16 guid = guid.replace("-", "");
17 return guid.toUpperCase();
18 }
19 
20 }

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文由星彩网app下载发布于星彩网app下载,转载请注明出处:Python完成将json文件中向量写入Excel的章程

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。