Python和ElasticSearch:使用索引将CSV转换为JSON
我想用Python将一堆CSV文件转换为特定的.JSON文件格式 这是我的示例CSV文件:Python和ElasticSearch:使用索引将CSV转换为JSON,python,
elasticsearch,Python,
elasticsearch,我想用Python将一堆CSV文件转换为特定的.JSON文件格式 这是我的示例CSV文件: L1-CR109 Security Counter,has been forced,2019-02-26 L1-CR109 Security Counter,has been forced,2019-02-26 L1-CR109 Security Counter,has been forced,2019-02-26 L1-CR109 Security Counter,has been forced,201
L1-CR109 Security Counter,has been forced,2019-02-26
L1-CR109 Security Counter,has been forced,2019-02-26
L1-CR109 Security Counter,has been forced,2019-02-26
L1-CR109 Security Counter,has been forced,2019-02-26
。。这是我想要的json输出:
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "location" : "L1-CR109 Security Counter", "door_activity": "has been forced", "2019-02-26"}
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "location" : "L1-CR109 Security Counter", "door_activity": "has been forced", "2019-02-26"}
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "location" : "L1-CR109 Security Counter", "door_activity": "has been forced", "2019-02-26"}
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "location" : "L1-CR109 Security Counter", "door_activity": "has been forced", "2019-02-26"}
目前,我能够生成以下json格式的结果:
[{"location": "L1-CR109 Security Counter", "door_status": "has been forced", "date": "2019-02-21"},
{"location": "L1-CR109 Security Counter", "door_status": "has been forced", "date": "2019-02-21"},
{"location": "L1-CR109 Security Counter", "door_status": "has been forced", "date": "2019-02-21"},
{"location": "L1-CR109 Security Counter", "door_status": "has been forced", "date": "2019-02-21"}
…这是我的Python代码:
def csv_to_json():
in_file = '/Elastic Search/Converted Detection/Converted CSV'
out_file = '/Elastic Search/Converted Detection/Converted JSON'
for filename in os.listdir(in_file):
print("\n")
print("Converting " + filename + " file...")
with open(in_file + "/" + filename, 'r') as f:
if filename.endswith(".csv"):
reader = csv.DictReader(f, fieldnames=("location", "door_status", "date"))
out = json.dumps([row for row in reader])
text_file = open(out_file + r'/{}.json'.format(filename[:-4]), 'w')
text_file.write(out + "\n")
我曾试图寻找解决办法,但无济于事。我可以知道我在代码中遗漏了什么吗?我可以再次咨询一下,为什么弹性搜索只允许我想要的json输出格式带有索引,而不是普通的python格式吗?这是一种方法。注意-您没有为日期字段指定名称,所以我这样做是为了使其有效(json)
这是一种方法。注意-您没有为日期字段指定名称,所以我这样做是为了使其有效(json)
下面是Python pandas包的一个版本:
import json
import pandas as pd
in_file = '/Elastic Search/Converted Detection/Converted CSV'
out_file = '/Elastic Search/Converted Detection/Converted JSON'
index_line = '{"index": {"_index": "test", "_type": "_doc", "_id": "1"}}\n'
阅读:
df = pd.read_csv(in_file)
或直接从字符串:
text = "L1-CR109 Security Counter,has been forced,2019-02-26\n"*4
df = pd.read_csv(StringIO(text),header=None)
现在编写所需的格式(注意,我添加了'date',因此它是一个有效的JSON):
下面是Python pandas包的一个版本:
import json
import pandas as pd
in_file = '/Elastic Search/Converted Detection/Converted CSV'
out_file = '/Elastic Search/Converted Detection/Converted JSON'
index_line = '{"index": {"_index": "test", "_type": "_doc", "_id": "1"}}\n'
阅读:
df = pd.read_csv(in_file)
或直接从字符串:
text = "L1-CR109 Security Counter,has been forced,2019-02-26\n"*4
df = pd.read_csv(StringIO(text),header=None)
现在编写所需的格式(注意,我添加了'date',因此它是一个有效的JSON):
你可以从中得到一些灵感,你可以从和中得到一些灵感