Làm cách nào để nối dữ liệu JSON vào danh sách trong Python?

Để chuyển đổi Chuỗi JSON thành Danh sách Python, hãy sử dụng json. chức năng tải []. hàm loading[] lấy chuỗi JSON Array làm đối số và trả về Danh sách Python

cú pháp

Cú pháp sử dụng json. phương thức tải [] là

import json

aList = json.dumps[jsonString]

Chúng ta phải nhập gói json để sử dụng json. hàm bãi []

Ghi chú. Xin lưu ý rằng hàm dumps[] trả về Danh sách Python, chỉ khi chuỗi JSON là Mảng JSON

ví dụ 1. Chuyển đổi chuỗi mảng JSON thành danh sách Python

Trong ví dụ này, chúng tôi sẽ lấy một chuỗi JSON Array và chuyển đổi nó thành Danh sách Python. Mảng JSON có hai phần tử, với mỗi phần tử chứa hai khóa. cặp giá trị của mỗi

Sau khi tải chuỗi JSON vào danh sách, chúng ta sẽ in giá trị cho khóa “b”

Chương trình Python

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
Chạy

đầu ra

2

ví dụ 2. Chuyển đổi mảng JSON của chuỗi mảng thành danh sách Python

Trong ví dụ này, chúng tôi sẽ lấy một Chuỗi JSON với Mảng của Mảng và chuyển đổi nó thành Danh sách Danh sách của Python

Sau khi phân tích cú pháp chuỗi JSON thành Danh sách Python, chúng ta sẽ truy cập phần tử đầu tiên của phần đầu tiên trong toàn bộ danh sách

❮ Liệt kê các phương thức

Thí dụ

Thêm một phần tử vào danh sách fruits

trái cây = ['táo', 'chuối', 'anh đào']
hoa quả. nối thêm ["màu cam"]

Tự mình thử »

Định nghĩa và cách sử dụng

Phương thức append[] nối một phần tử vào cuối danh sách

cú pháp

Giá trị tham số

Tham sốMô tảelmntBắt buộc. Một phần tử thuộc bất kỳ loại nào [chuỗi, số, đối tượng, v.v. ]

Thêm ví dụ

Thí dụ

Thêm danh sách vào danh sách

a = ["táo", "chuối", "anh đào"]
b = ["Ford", "BMW", "Volvo"]
a. nối thêm [b]

Tự mình thử »

❮ Liệt kê các phương thức


Tôi đã cố gắng nhận trợ giúp cho cùng một vấn đề về lỗi tràn ngăn xếp nhưng không nhận được trợ giúp hoặc phản hồi nào. Tôi đang đăng lại ở đây với hy vọng rằng ai đó có thể vui lòng hướng dẫn tôi vì tôi không thể đẩy mã vào kho lưu trữ do chậm trễ

Mã của tôi


import json
import re
from http.client import responses

import vt
import requests

with open['/home/asad/Downloads/ssh-log-parser/ok.txt', 'r'] as file:
file = file.read[]

pattern = re.compile[r'\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}']
ips = pattern.findall[file]
unique_ips = list[set[ips]]

# print[unique_ips]

# print[len[unique_ips]]

headers = {
"accept": "application/json",
"x-apikey": "9765ba5d9fd52f5747dde5240606019f83d32758cb664abc63a43488aa42812d"
}
i = 0
url = "//www.virustotal.com/api/v3/ip_addresses/"

# messages = \[\]

f = open['formater.json']

# returns JSON object as

# a dictionary

data = json.load[f]
f.close[]
no = 0
while i \< len[unique_ips]:

    furl = url + str[unique_ips[i]]
    # response = requests.get[furl, headers=headers]
    
    # data_ = response.json[]
    
    # print[data_]
    
    # messages = [data_['data']['attributes']['last_analysis_results']]
    messages = [data['data']['attributes']['last_analysis_results']]
    
    y = json.dumps[messages]
    y1 = json.loads[y]
    
    # print[y1]
    a = []
    r = []
    v = []
    cnt = 0
    store = len[y1[0]]
    out_json_new = []
    out_json1 ={}
    
    
    for o in y1:
            for k, vv in o.items[]:
    
                a_ = vv['result']
                a.append[a_]
    
                r_ = vv['engine_name']
                r.append[r_]
    
                v_ = vv['category']
    
                v.append[v_]
    
                out_json = {
                    "indicators": [{
                        "value": str[unique_ips[i]],
                        "type": 'ip',
    
                    }]
                }
    
                out_json1 ={
                                       "providers":[{
                                       "provider": str[r],
                                                 "verdict": str[a],
                                                 "score": str[v]
    
                                    }]             }
    
                out_json1['providers'].append[out_json1]
    
    i += 1
    
    print[out_json,out_json1]

\#for aaa in a:
\#print[a\[0\]]\`

Đầu ra như

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}

Tôi muốn thay đổi đầu ra sang định dạng này

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }

Mã hiện tại của tôi, các nhóm dưới một khóa e. g

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
7 thay vào đó, nó nên được thêm lần lượt như trong đầu ra ở trên. Tôi đã cố gắng sử dụng logic
{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
8 nhưng nó không hoạt động khi tôi tham dự. Nó xuất ra như

________số 8_______

{'chỉ số'. [{'giá trị'. ‘192. 91. 72. 101’, ‘loại’. 'ip'}]} {'nhà cung cấp'. [{'các nhà cung cấp'. “[‘Bkav’, ‘CMC Threat Intelligence’, 'CMC sarah']”, ‘phán quyết’. “['sạch', 'chân', 'ghét']", 'ghi điểm'. “['vô hại', 'vô hại', 'sarah']”}, {…}]}

TỆP LIÊN QUAN

Để chạy mã, các tệp này là bắt buộc
Vâng. txt

Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.201
Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.101

định dạng. json

{
"data": {
"attributes": {
"country": "US",
"last_analysis_stats": {
"harmless": 86,
"malicious": 0,
"suspicious": 0,
"undetected": 0,
"timeout": 0
},
"last_analysis_results": {
"Bkav": {
"category": "harmless",
"result": "clean",
"method": "blacklist",
"engine_name": "Bkav"
},
"CMC Threat Intelligence": {
"category": "harmless",
"result": "legs",
"method": "blacklist",
"engine_name": "CMC Threat Intelligence"
},
"CMC sarah ": {
"category": "sarah",
"result": "hate",
"method": "you",
"engine_name": "CMC sarah "
}
}
}
}
}

Bỏ qua các kết quả đầu ra hiện tại và mong muốn của bạn

 {'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}

mong muốn

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
 ]
 }

Đầu ra mong muốn của bạn là định dạng JSON. Nhưng đầu ra hiện tại của bạn không phải là
JSON. Nó thực sự là Python

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9 và
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
0 từ
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
1 này
gọi

 print[out_json,out_json1]

Vì vậy, những gì đang xảy ra hơi khó hiểu, bởi vì chúng khá giống nhau
Thực tế là bạn đã đặt tên cho các biến của mình là

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
2 chẳng ích gì
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
3 và tương tự. Chúng không phải là JSON, chúng đã cũ
Trăn
{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9s. Nhưng những cái tên sẽ gây hiểu lầm cho bạn

Vì vậy, hãy làm cứng một ví dụ nhỏ

 import json
 d = {'a': 1, 'b': [3,4,5]}
 print[d]
 print[json.dumps[d]]

Hãy mổ xẻ điều đó

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
0

Nhập mô-đun

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
5, vì những lý do rõ ràng

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
1

Xác định một

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9 nhỏ, với một mục nhập có khóa
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
7 có giá trị là
giá trị
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
8 và một mục có khóa 'b' có giá trị là danh sách
 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
9

Bây giờ,

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9 không phải là “ở định dạng JSON” hay bất kỳ định dạng nào thực sự. Nó chỉ là
cấu trúc dữ liệu. Nhưng cú pháp Python để viết ra một
{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9 như vậy
trực tiếp trông hơi JSONish

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
2

Nếu bạn đã viết giá trị đó ở [định dạng JSON][JSON - Wikipedia]
bạn có thể viết

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
3

và sự khác biệt rõ ràng nhất là dấu ngoặc kép. Nhưng JSON là
JavaScript tương thích. Dù sao, JSON một định dạng tệp cho
giá trị đọc/ghi. Bản thân biến

            `out_json1['providers'].append[out_json1]`
2 không phải là “ở định dạng JSON”
hoặc “Cú pháp Python”;

Hãy đi đến đầu ra

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
4

Khi bạn gọi

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
1, nó sẽ chuyển đổi từng đối số của nó thành một chuỗi,
và viết các chuỗi có khoảng cách giữa mỗi chuỗi. biến
            `out_json1['providers'].append[out_json1]`
2 là một
{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9, và
            `out_json1['providers'].append[out_json1]`
6 giống với
            `out_json1['providers'].append[out_json1]`
7 là
Cú pháp Python cho
{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
9. Do đó, đầu ra Pythonesque

Bây giờ là

 {
 "providers":\[
 {
 "provider":"['Bkav']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC Threat Intelligence']",
 "verdict":"['clean']",
 "score":"['harmless']"
 },
 {
 "provider":"['CMC sarah']",
 "verdict":"['hate']",
 "score":"['harmless']"
}
 ]
 }
1 thứ hai

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
5

Khi nó chạy, nó in cái này

{'indicators': [{'value': '192.91.72.201', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
 {'indicators': [{'value': '192.91.72.101', 'type': 'ip'}]} {'providers': [{'provider': "['Bkav', 'CMC Threat Intelligence', 'CMC sarah ']", 'verdict': "['clean', 'legs', 'hate']", 'score': "['harmless', 'harmless', 'sarah']"}, {...}]}
6

Đó là bởi vì

Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.201
Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.101
0 trả về một chuỗi có giá trị trong JSON
định dạng. AND print chuyển đổi chuỗi đó thành… chính xác chuỗi đó,
bởi vì nó đã là một chuỗi

Bạn muốn ngắt dòng trong đầu ra của mình. Nhìn vào các tài liệu cho

Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.201
Aug 22 09:45:08 ip-170-32-23-64 sshd\[1546\]: Invalid user HPSupport from 192.91.72.101
1 đây

tài liệu Python

Mã nguồn. Lib/json/__init__. py JSON [Ký hiệu đối tượng JavaScript], được chỉ định bởi RFC 7159 [đã lỗi thời RFC 4627] và bởi ECMA-404, là một định dạng trao đổi dữ liệu nhẹ lấy cảm hứng từ JavaScript

Làm cách nào để cập nhật dữ liệu trong tệp JSON bằng Python?

Cập nhật đối tượng JSON trong Python cũng đơn giản như sử dụng hàm update[] tích hợp sẵn từ gói json mà chúng ta đã nhập . Phương thức cập nhật được sử dụng để thêm một cặp khóa-giá trị mới vào chuỗi JSON mà chúng tôi đã khai báo trong mã của mình.

Chúng ta có thể thêm danh sách vào JSON không?

JSON có thể đại diện cho hai loại cấu trúc như đối tượng và mảng. Một đối tượng là một tập hợp các cặp khóa/giá trị không có thứ tự và một mảng là một chuỗi các giá trị có thứ tự. Chúng ta có thể chuyển đổi danh sách thành mảng JSON bằng cách sử dụng JSONArray .

Làm cách nào để thêm giá trị khóa trong đối tượng JSON Python?

nhập json với open["your_json_file. txt", 'r'] dưới dạng f. dữ liệu = json. tải [f. read[]] #data trở thành từ điển #làm mọi việc với dữ liệu ở đây data['ADDED_KEY'] = 'ADDED_VALUE' #và sau đó chỉ cần ghi lại dữ liệu vào tệp bằng open["your_json_file.

Làm cách nào để cập nhật dữ liệu JSON?

Để cập nhật dữ liệu JSON trong bảng. .
Truy vấn và truy xuất dữ liệu JSON từ cột JSON
Sửa đổi nội dung của cột JSON
Để đẩy giá trị cột đã cập nhật trở lại bảng, hãy đưa ra câu lệnh UPDATE SQL và chỉ định hàm JSON2BSON

Chủ Đề