<!————-知识的力量是无限的(当然肯定还有更简约的章程)———–!>

sqlyog导出json数据格式匡助mysql数据转存mongodb,jsonmongodb

<!————-知识的力量是极端的(当然肯定还有更简短的办法)———–!>

 

当自己着想将省市区三级联动数据从mysql转入mongodb时遇见了网上无直接插入mongodb的言传身教(基本均是mysql插入示例)。于是想到利用json文件直接导入mongodb会比较easy(SQLyog怎么着导出json?)

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

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

挑选复制所有行到剪贴板:

澳门金沙国际 3

澳门金沙国际 4

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

json在线校验URL: 

json在线压缩URL:

澳门金沙国际 5澳门金沙国际 6

末段以压缩成一个json文件的格局在java代码中分析

澳门金沙国际 7

主干精华:

@Test
public void testProCityArea(){
String fileName = "ProvCityArea.geojson";
String path = System.getProperty("user.dir") + "\\src\\main\\webapp\\static\\geojson\\" + fileName;
JSONObject jsonobject = JSONObject.parseObject(FileHelper.readFile(path));
JSONArray provArray = jsonobject.getJSONArray("provinces");
for (Object object : provArray) {
JSONObject provJson = (JSONObject) object;
Province province = new Province(GuidUtils.getInstance().getGuid(), provJson.getString("code"), provJson.getString("name"));
mongoTemplate.insert(province, "province");
}
JSONArray cityArray = jsonobject.getJSONArray("city");
for (Object object : cityArray) {
JSONObject cityJson = (JSONObject) object;
City city = new City(GuidUtils.getInstance().getGuid(), cityJson.getString("code"), cityJson.getString("name"), cityJson.getString("provinceCode"));
mongoTemplate.insert(city, "city");
}
JSONArray areaArray = jsonobject.getJSONArray("area");
for (Object object : areaArray) {
JSONObject areaJson = (JSONObject) object;
Area area = new Area(GuidUtils.getInstance().getGuid(), areaJson.getString("code"), areaJson.getString("name"), areaJson.getString("cityCode"));
mongoTemplate.insert(area, "area");
}
}

FileHelper:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileHelper {

public static String readFile(String path){
BufferedReader reader = null;
String laststr = "";
try{
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while((tempString = reader.readLine()) != null){
laststr += tempString;
}
reader.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr;
}

}

GuidUtils

public class GuidUtils {

private static final GuidUtils instance = new GuidUtils();

private GuidUtils(){

}

public static GuidUtils getInstance() {
return instance;
}

public String getGuid() {
UUID uuid = UUID.randomUUID();
String guid = uuid.toString();
guid = guid.replace("-", "");
return guid.toUpperCase();
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Python完成将json文件中向量写入Excel的法子。
!————-知识的力量是然则的(当然肯定还有更不难的主意)———–!
当自家着想将省…

<!————-知识的力量是最为的(当然肯定还有更简单的方式)———–!>

正文实例讲述了Python已毕将json文件中向量写入Excel的点子。分享给我们供我们参考,具体如下:

 

 

有一个亟待是将曾经向量化的页面的数目中的向量写入Excel中用来显示,工作很简单,那里大致复习一下Excel文书写的做事,试验中应用的json文件内容如下:

当自身着想将省市区三级联动数据从mysql转入mongodb时遭受了网上无直接插入mongodb的示范(基本均是mysql插入示例)。于是想到利用json文件直接导入mongodb会相比较easy(SQLyog如何导出json?)

当我着想将省市区三级联动数据从mysql转入mongodb时相遇了网上无直接插入mongodb的以身作则(基本均是mysql插入示例)。于是想到利用json文件平昔导入mongodb会相比easy(SQLyog怎么着导出json?)

复制代码 代码如下:

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

在SQLyog中写一个json格式查询语句:(省市区example如下:)看一下就能懂其中规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0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 1, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 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, 1,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1,
0, 0, 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
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

代码如下:

 澳门金沙国际 8澳门金沙国际 9

 澳门金沙国际 10澳门金沙国际 11

#!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列来试验了

澳门金沙国际 12

澳门金沙国际 13

此地只是不难达成了写入Excel文件的效果,还有其余的法门就不多说了。

澳门金沙国际 14

澳门金沙国际 15

PS:关于json操作,那里再为大家推荐七款比较实用的json在线工具供大家参考运用:

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

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

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

json在线校验URL: 

json在线校验URL: 

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

json在线压缩URL:

json在线压缩URL:

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

澳门金沙国际 16澳门金沙国际 17

澳门金沙国际 18澳门金沙国际 19

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

最后以压缩成一个json文件的样式在java代码中剖析

末尾以压缩成一个json文件的款型在java代码中分析

在线json压缩/转义工具:

澳门金沙国际 20

澳门金沙国际 21

越来越多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 }
 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:

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 }
 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

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 }
 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 }

 

 

 

 

 

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章