Python и ElasticSearch: конвертировать CSV в JSON с индексом - PullRequest
0 голосов
/ 28 марта 2019

Я хотел бы преобразовать группу файлов CSV в определенный формат файлов .JSON в Python.

Это мой пример файла 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,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")

Я пытался найти решение, но безрезультатно.Могу ли я узнать, что мне не хватает в коде?Опять же, могу ли я получить совет о том, почему Elastic Search допускает только мой желаемый выходной формат json с индексом вместо обычного формата python?

Ответы [ 2 ]

1 голос
/ 29 марта 2019

Вот версия с пакетом Python:

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):

with open('outfile.json', 'w+') as outfile:
    for row in df.to_dict('records'):
       data = json.dumps(dict(list(zip(title,row.values()))))
       outfile.write(index_line+data)
1 голос
/ 28 марта 2019

Это один из способов сделать это.Примечание. Вы не указали в поле даты имя, поэтому я сделал это, чтобы сделать его действительным json).

import json
import csv
import sys
from collections import OrderedDict

index_line = { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
with open('input.csv', 'r') as infile, open('outfile.json', 'w+') as outfile:

    inreader = csv.reader(infile, delimiter=',', quotechar='"')

    for line in inreader:
        document = OrderedDict()
        document['location'] = line[0]
        document['door_activity'] = line[1]
        document['date'] = line[2]
        json.dump(index_line, outfile)
        outfile.write("\n")
        json.dump(document, outfile)
        outfile.write("\n")

sys.exit()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...