-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjsondata.py
More file actions
144 lines (133 loc) · 3.94 KB
/
jsondata.py
File metadata and controls
144 lines (133 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
用于对数据进行保存
函数:
check_code(data):'生成校验码'
test_check(data):'检验校验码是否正确,返回bool值'
save_data(data, json_data_file):'保存数据到文件'
read_data(data, json_data_file):'读数据'
变量:
# json数据文件名称/地址
json_data_file = 'data.json'
# 初始化数据
data = {
'program_name': 'Python program',#程序名
'program_path': sys.argv[0],# 程序路径
'verson': [0, 0, 0, 0],# 版本号
'autor': '小喾苦',# 制作人
'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),# 时间
'check_code': '',# 校验码
}
'''
# json轻量级的数据交换格式
import json
"""
json.dumps(var): 对数据进行编码。
json.loads(var): 对数据进行解码。
json.dump(data, file): 对文件的数据进行编码。
json.load(file): 对文件的数据进行解码。
JSON --> Python
object dict
array list
string str
number(int) int
number(real) float
true True
false False
null None
"""
import time
'''
# 格式化成2016-03-20 11:45:39形式
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
'''
# 使用MD5加密生成校验码
import hashlib
'hashlib.md5(string.encode()).hexdigest()'
# 用于获取argv
import sys
'print(sys.argv)'
# 用于文件操作
import os.path
# 对话框
from tkinter import messagebox
def check_code(dat):
# 浅拷贝,防止源数据效验码丢失
data = dat.copy()
'生成校验码'
# 尝试删除数据文件原先的校验码
try:
data.pop('check_code')
except:
pass
# 将数据改为以字符串格式
data = repr(data)
# 校验码是MD5加密后的结果
check = hashlib.md5(data.encode()).hexdigest()
# 返回校验码
return check
def test_check(data):
'检验校验码是否正确,返回bool值'
# 尝试获取原校验码
try:
checkcode = data['check_code']
except:
return False
# 获取校验码
check = check_code(data)
if checkcode == check:
return True
else:
return False
def save_data(data, json_data_file):
'保存数据到文件'
# 更新校验码
data['check_code'] = check_code(data)
# 写入 JSON 数据
with open(json_data_file, 'w') as f:
json.dump(data, f)
return data
def read_data(data, json_data_file):
'读数据'
# 判断数据文件是否存在
if os.path.isfile(json_data_file):
with open(json_data_file, 'r') as f:
new_data = json.load(f)
# print(new_data)
if test_check(new_data):
# print(new_data)
return new_data
else:
messagebox.showerror(title = '数据受损,无法读取!',message='数据受损,无法读取!\n请不要修改数据!')
save_data(data, json_data_file)
return data
else:
# print("文件")
save_data(data, json_data_file)
return data
# json数据文件名称/地址
json_data_file = 'data.json'
# 初始化数据
data = {
'program_name': 'Python program',#程序名
'program_path': sys.argv[0],# 程序路径
'verson': [0, 0, 0, 0],# 版本号
'autor': '小喾苦',# 制作人
'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),# 时间
'check_code': '',# 校验码
}
if __name__ == '__main__':
# json_str = json.dumps(data)
# print ("Python 原始数据:", repr(data))
# print ("JSON 对象:", json_str)
'''
"文件操作"
# 写入 JSON 数据
with open('data.json', 'w') as f:
json.dump(data, f)
'''
data = save_data(data, json_data_file)
# 读取数据
with open('data.json', 'r') as f:
data = json.load(f)
print(data)
print(test_check(data))