JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
# 写文件
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);
import json
import os
import subprocess
import shutil
import uuid
from loguru import logger
# 写文件
from SvgProcess.ACodeTimer import CodeTimer
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 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;
JSON 是轻量级的文本数据交换格式,对程序员比较友好。