【问题标题】:Retrieve specific data by splitting json data into chunks with python通过使用 python 将 json 数据拆分为块来检索特定数据
【发布时间】:2020-10-30 15:54:00
【问题描述】:

如何在下面的 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"}
]

【问题讨论】:

  • symbols = [x['symbol'] for x in data].

标签: python json python-3.x python-2.7


【解决方案1】:

您可以在将字典列表加载为 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)

【讨论】:

【解决方案2】:

这是一个字典列表。

import json

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

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

【讨论】:

【解决方案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() 

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

【讨论】:

  • 我在示例数据中没有看到 emp_details 键。
  • 那是因为它引用了我在底部添加的源代码...谢谢指出;我只是按照问题的要求对其进行了编辑以打印“符号”。
  • 非常感谢您提供的样品。我怎样才能用“pypi.org/project/Columnar”将其制表?
  • 你应该打开一个新问题来问这个问题,无论如何写这样的东西: data = [] table = columnar(data, headers=['Race', 'Date', 'Location', 'Distance '], patterns=patterns) print(table) 并将 print(i) 替换为 data.append(i)
猜你喜欢
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
相关资源
最近更新 更多