文章目录
- 1.json
- 2.类对象序列化
- 3.调用exe进行数据交换
- 4.总结
1.json
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
2.类对象序列化
# 写文件
def _WriteFile(filename, strData):data = strData.encode('UTF-8');# 打开一个文件fw = open(filename, "wb");fw.write(data);# 关闭打开的文件fw.close();# 读文件
def _ReadFile(filename):# 读取数据data = None;with open(filename, 'rb') as fr:data = fr.read();# 编码转换try:strData = str(data, 'utf-8');return strData;except:try:strData = str(data, 'GBK');return strData;except Exception as e:try:strData = str(data, 'latin1');return strData;except Exception as e:print(filename);pass;pass;return None;class AppPara:def __init__(self):self.svg_show_url = ""; # svg显示的二维码网址self.svg_show_local_full_filename = ""; # svg结果服务器保存路径self.svg_download_result_full_filename = ""; # 压缩包结果保存路径self.svg_upload_para_full_filename = ""; # 程序初始化压缩包路径self.svg_color_table_full_filename = ""; # 色表文件路径def DumpFile(self, filename):# 类对象转为json字符串data = json.dumps(self, default=lambda obj: obj.__dict__, ensure_ascii=False);# 写入文件_WriteFile(filename, data);def LoadFile(self, filename):# 读取文件数据data = _ReadFile(filename);if data is None:return False;# 字典对象转数据objDict = json.loads(data);self.__dict__.update(objDict);
3.调用exe进行数据交换
import json
import os
import subprocess
import shutil
import uuid
from loguru import logger# 写文件
from SvgProcess.ACodeTimer import CodeTimerdef _WriteFile(filename, strData):data = strData.encode('UTF-8');# 打开一个文件fw = open(filename, "wb");fw.write(data);# 关闭打开的文件fw.close();# 读文件
def _ReadFile(filename):# 读取数据data = None;with open(filename, 'rb') as fr:data = fr.read();# 编码转换try:strData = str(data, 'utf-8');return strData;except Exception as e:try:strData = str(data, 'GBK');return strData;except Exception as e:try:strData = str(data, 'latin1');return strData;except Exception as e:logger.error("PolygonMultiCenter _ReadFile:" + str(e));print(filename);return None;# 将文件序列化为json字符串
def ToJson(tmpParaFilename, pathList, colorAreaDictNum, textPtWidthHeightMMInfoFilename, resultJsonFilename):# 抽取路径对象pathJsonObjList = [];for pathObj in pathList:pathJsonObj = {"Index": pathObj.m_Index,"PointsMM": pathObj.m_pointsMM,"ColorTableIndexText": pathObj.m_ColorTableIndexText};pathJsonObjList.append(pathJsonObj);# 构建json对象jsonObj = {"colorAreaDictNum": str(colorAreaDictNum),"textPtWidthHeightMMInfoFilename": textPtWidthHeightMMInfoFilename,"pathJsonObjList": pathJsonObjList,"resultJsonFilename": resultJsonFilename};# 类对象转为json字符串data = json.dumps(jsonObj);# 写入文件_WriteFile(tmpParaFilename, data);pass;def ComputeExe(svgfilename, pathList, colorAreaDictNum):# 构建json文件和字体信息文件rootPath = os.path.dirname(__file__);loc_json_fmt = r"{}/tmp/" + str(uuid.uuid1()).replace("-", "") + ".json"; # 参数文件loc_json_filename = loc_json_fmt.format(rootPath);loc_json_filename = loc_json_filename.replace("\\", "/");loc_textPtWH_fmt = r"{}/TextPtWidthHeightMMInfo.json"; # 文本宽高信息loc_textPtWH_filename = loc_textPtWH_fmt.format(rootPath);loc_textPtWH_filename = loc_textPtWH_filename.replace("\\", "/");result_json_fmt = r"{}/tmp/" + str(uuid.uuid1()).replace("-", "") + "-PMC.json"; # 标注结果result_json_filename = result_json_fmt.format(rootPath);result_json_filename = result_json_filename.replace("\\", "/");# 生成参数文件ToJson(loc_json_filename, pathList, colorAreaDictNum, loc_textPtWH_filename, result_json_filename);_Timer = CodeTimer("计算时间");exe_fmt = r"{}/PolygonMultiCenter/PolygonMultiCenter.exe"; # 可执行文件exe_filename = exe_fmt.format(rootPath);exe_filename = exe_filename.replace("\\", "/");# 调用第三方程序实现多点标注_command_str = "\"" + exe_filename + "\"" + " \"" + loc_json_filename + "\"" # 编辑命令行ex = subprocess.Popen(_command_str, stdout=subprocess.PIPE, shell=True);out, err = ex.communicate();status = ex.wait();#_Timer.End();if not os.path.exists(result_json_filename):logger.error("结果未生成:"+svgfilename);return None;# 读取结果data = _ReadFile(result_json_filename)labelTextList = json.loads(data);# 删除临时文件os.remove(loc_json_filename);os.remove(result_json_filename);return labelTextList;
4.总结
JSON 是轻量级的文本数据交换格式,对程序员比较友好。