Json - QLGQ/learning-python GitHub Wiki
存储json文档
filepath = sys.path[0] + "page_js_info.txt"
with open(filepath, 'w', encoding='utf-8') as tmpFile:
tmpFile.write(json.dumps(page_js_infos, ensure_ascii=False, indent=4))
page_js_infos为已知的json文本。
读取json文档
filePath = sys.path[0] + "page_js_info.txt"
with open(filePath, 'r', encoding='utf-8') as tmpFile:
jsonData = json.load(tmpFile)
js_names = []
for k, v in jsonData.items():
page_js_datas = v.get('page_js_datas')
for x in page_js_datas:
js_names.append(x.get("js_content"))
写list到txt文件
ipTable = ['158.59.194.213', '18.9.14.13', '58.59.14.21']
fileObject = open('sampleList.txt', 'w')
for ip in ipTable:
fileObject.write(ip)
fileObject.write('\n')
fileObject.close()
不能直接将list或dict对象进行写入,会出现typeError。
写dict对象到json文件
import json
dictObj = {
'andy':{
'age': 23,
'city': 'shanghai',
'skill': 'python'
},
'william': {
'age': 33,
'city': 'hangzhou',
'skill': 'js'
}
}
jsObj = json.dumps(dictObj)
fileObject = open('jsonFile.json', 'w')
fileObject.write(jsObj)
fileObject.close()
需要import json,将dict转为字符串后写入json文件。
DEMO
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
import os
import sys
rootdir = 'D:\python_work\MQSTATIC-python\web'
def count_js():
dirList = os.listdir(rootdir)
HtmlList = []
for file_name in dirList:
if file_name.endswith(".html"):
fileName = rootdir + os.sep + file_name
HtmlList.append(fileName)
page_js_infos = {}
for html_path in HtmlList:
page_js_info = {}
with open(html_path, 'r', encoding='utf-8') as tmp_file:
html_name = str(html_path).split("web\\")[1]
page_js_datas = []
for (num, html_line) in enumerate(tmp_file):
pattern = re.compile(r'src=(.*\.js)')
match = pattern.findall(html_line)
if len(match) > 0:
js_info = {
"line_num": num,
"js_content": match[0]
}
page_js_datas.append(js_info)
page_js_count = len(page_js_datas)
page_js_info.update({
html_name: {
"count": page_js_count,
"page_js_datas": page_js_datas
}
})
page_js_infos.update(page_js_info)
# print(page_js_infos)
filePath = sys.path[0] + "page_js_info.txt"
with open(filePath, 'w', encoding='utf-8') as tmpFile:
tmpFile.write(json.dumps(page_js_infos, ensure_ascii=False, indent=4))
def all_js_name():
filePath = sys.path[0] + "page_js_info.txt"
with open(filePath, 'r', encoding='utf-8') as tmpFile:
jsonData = json.load(tmpFile)
js_names = []
for k, v in jsonData.items():
page_js_datas = v.get('page_js_datas')
for x in page_js_datas:
js_names.append(x.get("js_content"))
return set(js_names), len(set(js_names))
def get_html_js_info():
filePath = sys.path[0] + "page_js_info.txt"
with open(filePath, 'r', encoding='utf-8') as tmpFile:
jsonData = json.load(tmpFile)
return jsonData
def js_count():
js_names, count = all_js_name()
html_js_info = get_html_js_info()
js_count_info = {}
for js_name in js_names:
js_count_datas = []
for k, v in html_js_info.items():
print(k)
print(v)
page_js_datas = v.get('page_js_datas')
for page_js_data in page_js_datas:
line_num = page_js_data.get('line_num')
js_content = page_js_data.get('js_content')
if js_name == js_content:
js_data = {
'html_name': k,
'num': line_num
}
js_count_datas.append(js_data)
if len(js_count_datas) < 3:
continue
js_count_info.update({
js_name: {
'count': len(js_count_datas),
'count_datas': js_count_datas
}
})
print(js_count_info)
filePath = sys.path[0] + "js_count_info.txt"
with open(filePath, 'w', encoding='utf-8') as tmpFile:
tmpFile.write(json.dumps(js_count_info, ensure_ascii=False, indent=4))
# all_js_name()
# count_js()
js_count()