代码:
import json
# 对Json解码
test = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
print("===" * 20)
print(test)
print("===" * 20)
print(type(test))
dict = '{"foo":"bar", "json":"loads"}'
test_dict = json.loads(dict)
print("===" * 20)
print(test_dict)
print("===" * 20)
print(type(test_dict))
运行结果:
============================================================
['foo', {'bar': ['baz', None, 1.0, 2]}]
============================================================
<class 'list'>
============================================================
{'foo': 'bar', 'json': 'loads'}
============================================================
<class 'dict'>
代码:
f = open("items.json")
json_line = next(f)
print("===" * 20)
print(json_line)
print("===" * 20)
print(type(json_line))
# 从字符串形式转换为字典形式
json_load = json.loads(json_line)
print("===" * 20)
print(json_load)
print("===" * 20)
print(type(json_load))
# 字典操作
print("===" * 20)
print(json_load["title"])
print("===" * 20)
print(json_load["content"])
print("===" * 20)
print(type(json_load["content"]))
print("===" * 20)
json_load["self_channel"] = "社会"
print(json_load.keys())
print(json_load["self_channel"])
运行结果:
============================================================
{"media_name": "证券时报", "source_time": 1522719180, "title": "*ST华泽复牌连续10日跌停", "scrapy_time": 1522720006, "site_id": 1, "source_channel": " 国内新闻", "source_url": "http://news.sina.com.cn/c/2018-04-03/doc-ifyswxnq1702174.shtml", "content": "<div class=\"article\" id=\"article\">\n\t\t\t\t<div><p>证券时报网(www.stcn.com)04月03日讯</p> <p>ST华泽复牌连续10日跌停,报7.5元,仍有逾70万手卖单封板。公司预计2017年亏损14-19亿元,公司股票将因连续三年亏损被暂停上市。</p><p>华商基金今年2月发布公告称,自2018年2月1日起,对旗下基金持有的*ST华泽股票进一步进行估值调整,调整后的估值价格为0.55元,相当于复牌后有连续61个跌停。</p> <p>(证券时报网快讯中心)</p> </div>\n\t\t\t\t\n\t\t\t\t </div>"}
============================================================
<class 'str'>
============================================================
{'media_name': '证券时报', 'source_time': 1522719180, 'title': '*ST华泽复牌连续10日跌停', 'scrapy_time': 1522720006, 'site_id': 1, 'source_channel': ' 国内新闻', 'source_url': 'http://news.sina.com.cn/c/2018-04-03/doc-ifyswxnq1702174.shtml', 'content': '<div class="article" id="article">\n\t\t\t\t<div><p>证券时报网(www.stcn.com)04月03日讯</p> <p>ST华泽复牌连续10日跌停,报7.5元,仍有逾70万手卖单封板。公司预计2017年亏损14-19亿元,公司股票将因连续三年亏损被暂停上市。</p><p>华商基金今年2月发布公告称,自2018年2月1日起,对旗下基金持有的*ST华泽股票进一步进行估值调整,调整后的估值价格为0.55元,相当于复牌后有连续61个跌停。</p> <p>(证券时报网快讯中心)</p> </div>\n\t\t\t\t\n\t\t\t\t </div>'}
============================================================
<class 'dict'>
============================================================
*ST华泽复牌连续10日跌停
============================================================
<div class="article" id="article">
<div><p>证券时报网(www.stcn.com)04月03日讯</p> <p>ST华泽复牌连续10日跌停,报7.5元,仍有逾70万手卖单封板。公司预计2017年亏损14-19亿元,公司股票将因连续三年亏损被暂停上市。</p><p>华商基金今年2月发布公告称,自2018年2月1日起,对旗下基金持有的*ST华泽股票进一步进行估值调整,调整后的估值价格为0.55元,相当于复牌后有连续61个跌停。</p> <p>(证券时报网快讯中心)</p> </div>
</div>
============================================================
<class 'str'>
============================================================
dict_keys(['media_name', 'source_time', 'title', 'scrapy_time', 'site_id', 'source_channel', 'source_url', 'content', 'self_channel'])
社会
代码:
# 还原到json格式incode,把字典格式转换成json格式
json_dump = json.dumps(json_load)
print(json_dump)
print("===" * 20)
print(type(json_dump))
print("===" * 20)
# 相互转换无障碍
print(json.loads(json_dump))
# unicode编码保存
fw = open("items_save.json", "w")
fw.write(json_dump)
fw.close()
# 中文编码保存
json_dump = json.dumps(json_load, ensure_ascii=False)
fw = open("iems_save_2.json", "wb")
fw.write(json_dump.encode("utf-8"))
fw.close()
运行结果:
{"media_name": "\u8bc1\u5238\u65f6\u62a5", "source_time": 1522719180, "title": "*ST\u534e\u6cfd\u590d\u724c\u8fde\u7eed10\u65e5\u8dcc\u505c", "scrapy_time": 1522720006, "site_id": 1, "source_channel": " \u56fd\u5185\u65b0\u95fb", "source_url": "http://news.sina.com.cn/c/2018-04-03/doc-ifyswxnq1702174.shtml", "content": "<div class=\"article\" id=\"article\">\n\t\t\t\t<div><p>\u8bc1\u5238\u65f6\u62a5\u7f51\uff08www.stcn.com\uff0904\u670803\u65e5\u8baf</p> <p>ST\u534e\u6cfd\u590d\u724c\u8fde\u7eed10\u65e5\u8dcc\u505c\uff0c\u62a57.5\u5143\uff0c\u4ecd\u6709\u903e70\u4e07\u624b\u5356\u5355\u5c01\u677f\u3002\u516c\u53f8\u9884\u8ba12017\u5e74\u4e8f\u635f14-19\u4ebf\u5143\uff0c\u516c\u53f8\u80a1\u7968\u5c06\u56e0\u8fde\u7eed\u4e09\u5e74\u4e8f\u635f\u88ab\u6682\u505c\u4e0a\u5e02\u3002</p><p>\u534e\u5546\u57fa\u91d1\u4eca\u5e742\u6708\u53d1\u5e03\u516c\u544a\u79f0\uff0c\u81ea2018\u5e742\u67081\u65e5\u8d77\uff0c\u5bf9\u65d7\u4e0b\u57fa\u91d1\u6301\u6709\u7684*ST\u534e\u6cfd\u80a1\u7968\u8fdb\u4e00\u6b65\u8fdb\u884c\u4f30\u503c\u8c03\u6574\uff0c\u8c03\u6574\u540e\u7684\u4f30\u503c\u4ef7\u683c\u4e3a0.55\u5143\uff0c\u76f8\u5f53\u4e8e\u590d\u724c\u540e\u6709\u8fde\u7eed61\u4e2a\u8dcc\u505c\u3002</p> <p>\uff08\u8bc1\u5238\u65f6\u62a5\u7f51\u5feb\u8baf\u4e2d\u5fc3\uff09</p> </div>\n\t\t\t\t\n\t\t\t\t </div>", "self_channel": "\u793e\u4f1a"}
============================================================
<class 'str'>
============================================================
{'media_name': '证券时报', 'source_time': 1522719180, 'title': '*ST华泽复牌连续10日跌停', 'scrapy_time': 1522720006, 'site_id': 1, 'source_channel': ' 国内新闻', 'source_url': 'http://news.sina.com.cn/c/2018-04-03/doc-ifyswxnq1702174.shtml', 'content': '<div class="article" id="article">\n\t\t\t\t<div><p>证券时报网(www.stcn.com)04月03日讯</p> <p>ST华泽复牌连续10日跌停,报7.5元,仍有逾70万手卖单封板。公司预计2017年亏损14-19亿元,公司股票将因连续三年亏损被暂停上市。</p><p>华商基金今年2月发布公告称,自2018年2月1日起,对旗下基金持有的*ST华泽股票进一步进行估值调整,调整后的估值价格为0.55元,相当于复牌后有连续61个跌停。</p> <p>(证券时报网快讯中心)</p> </div>\n\t\t\t\t\n\t\t\t\t </div>', 'self_channel': '社会'}
代码:
# 总结
import json
# 解析文件
# 打开文件
f = open("items_save.json", "r")
# 第一行
first_line = next(f)
# 解析得到一个字典,对第一行进行一个json格式的解析
dict_line = json.loads(first_line)
# 可以对字典进行操作
dict_line["self_channel"] = "社会/民生"
# 转换成字符串形式
json_dump = json.dumps(dict_line, ensure_ascii=False)
# 打开一个文件写入
fw = open("items_save_3.json", "w")
fw.write(json_dump)
fw.close()