通过使用python将json数据拆分为块来检索特定数据

2024-09-27 21:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在json数据中获取“符号”数据,如下图所示?我正在使用Python

另外,如何用replace替换此数据中的“}”对象

[
{"symbol": "ZILUSDT", "positionAmt": "0", "entryPrice": "0.00000", "markPrice": "0.01728152", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "SHORT"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "BOTH"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "LONG"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "SHORT"}
]

Tags: 数据falsesymbolcrossunrealizedprofitleverageentrypricepositionamt
3条回答

要读取json数据,请使用json.load()函数

# Python program to read 
# json file 


import json 

# Opening JSON file 
f = open('data.json',) 

# returns JSON object as 
# a dictionary 
data = json.load(f) 

# Iterating through the json 
# list 
for i in data['symbol']: 
    print(i) 

# Closing file 
f.close() 

Source https://www.geeksforgeeks.org/read-json-file-using-python/

在将字典列表作为JSON加载后,可以对其进行迭代(假设它是d):

print(*[x['symbol'] for x in d],sep="\n")

可读方式:

for x in d:
  print(x['symbol'])

从文件加载:

import json
with open('json.txt') as f:
  d = json.load(f)

那是一份字典清单

import json

with open('myfile.json') as file:
    items = json.load(file)

for item in items:
    print(item['symbol'])

相关问题 更多 >

    热门问题