python json数据写入文件的方法代码

介绍

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。

JSON在python中分别由list和dict组成。

这是用于序列化的两个模块:

json:用于字符串和python数据类型间进行转换

pickle:用于python特有的类型和python的数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

dumps : 把数据类型转换成字符串

dump : 把数据类型转换成字符串并存储在文件中

loads : 把字符串转换成数据类型

load : 把文件打开从字符串转换成数据类型

区别:

json是可以在不同语言之间交换数据的,而pickle只在python之间使用。

json只能序列化最基本的数据类型,josn只能把常用的数据类型序列化(列表、字典、列表、字符串、数字、),但不能是日期格式、类对象等。而pickle可以序列化所有的数据类型,包括类,函数都可以序列化。

Python将json文件写入ES数据库的方法

1、安装Elasticsearch数据库

PS:在此之前需首先安装Java SE环境

下载elasticsearch-6.5.2版本,进入/elasticsearch-6.5.2/bin目录,双击执行elasticsearch.bat 打开浏览器输入http://localhost:9200 显示以下内容则说明安装成功

安装head插件,便于查看管理(还可以用kibana)

首先安装Nodejs(下载地址https://nodejs.org/en/)

再下载elasticsearch-head-master包解压到/elasticsearch-6.5.2/下(链接: https://pan.baidu.com/s/1q3kokFhpuJ2Q3otPgu7ldg

提取码: 1rpp

修改配置文件elasticsearch-6.5.2\config\elasticsearch.yml如下:

进入elasticsearch-head-master目录下执行npm install -g grunt-cli,再执行npm install安装依赖

在elasticsearch-head-master目录下找到Gruntfile.js文件修改服务器监听地址如下:

执行grunt server命令启动head服务

访问地址 http://localhost:9100/ 即可访问head管理页面

2、将json文件写入ES数据库(py脚本如下)

# -*- coding: UTF-8 -*-
from itertools import islice
import json , sys
from elasticsearch import Elasticsearch , helpers
import threading
_index = 'indextest' #修改为索引名
_type = 'string' #修改为类型名
es_url = 'http://192.168.116.1:9200/' #修改为elasticsearch服务器
reload(sys)
sys.setdefaultencoding('utf-8')
es = Elasticsearch(es_url)
es.indices.create(index=_index, ignore=400)
chunk_len = 10
num = 0
def bulk_es(chunk_data):
bulks=[]
try:
for i in xrange(chunk_len):
bulks.append({
"_index": _index,
"_type": _type,
"_source": chunk_data[i]
})
helpers.bulk(es, bulks)
except:
pass
with open(sys.argv[1]) as f:
while True:
lines = list(islice(f, chunk_len))
num =num +chunk_len
sys.stdout.write('\r' + 'num:'+'%d' % num)
sys.stdout.flush()
bulk_es(lines)
if not lines:
print "\n"
print "task has finished"
break

总结

以上所述是小编给大家介绍的Python将json文件写入ES数据库的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对码农之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python遍历文件夹 处理json文件的方法

有两种做法:os.walk()、pathlib库,个人感觉pathlib库的path.glob用来匹配文件比较简单。

下面是第二种做法的实例(第一种做法百度有很多文章):

from pathlib import Path
import json
analysis_root_dir = "D:\\analysis_data\json_file"
store_result="D:\\analysis_data\\analysis_result\\dependency.csv"
def parse_dir(root_dir):
path = Path(root_dir)
all_json_file = list(path.glob('**/*.json'))
parse_result = []
for json_file in all_json_file:
# 获取所在目录的名称
service_name = json_file.parent.stem
with json_file.open() as f:
json_result = json.load(f)
json_result["service_name"] = service_name
parse_result.append(json_result)
return parse_result
def write_result_in_file(write_path , write_content):
with open(write_path,'w') as f:
f.writelines("service_name,action,method,url\n")
for dict_content in write_content:
url = dict_content['url']
method = dict_content['method']
action = dict_content['action']
service_name = dict_content['service_name']
f.writelines(service_name + ","+ action+","+method + ","+ url+"\n")
def main():
print("main begin...")
parse_result = parse_dir(analysis_root_dir)
print(parse_result)
write_result_in_file(store_result,parse_result)
print("main finished...")
if __name__ == '__main__':
main()

运行结果

main begin...
[{'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService01'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService02'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService03'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService04'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService05'}]
main finished...
目录结构
json file内容
{
"url":"/rest/webservice/v1/dosomthing",
"method":"post",
"action":"create"
}