νλ‘κ·Έλλ° μΈμ΄/Python
νμ΄μ¬μμ json νμ±νκΈ°
μμλπ
2023. 5. 18. 11:30
νμ΄μ¬μμ json νμΌμ μ½κ³ νμ± ν λλ¦μ νν°λ§μ κ±°μ³ λ³΄κ³ μΆμ μ 보λ₯Ό μΆλ ₯νλ κ°λ¨ν μμ λ€. νμ΄μ¬μμλ jsonμ jsμ κ°μ΄ νΈνκ² μ¬μ©ν μ μμΌλ©°, λ³λμ μΆκ° ν¨ν€μ§ μμ΄ jsonμ μ½κ³ μΈμ μλλ‘ μ§μνλ€.
λ³΄ν΅ jsonμ νμ΄μ¬μ 리μ€νΈ, νν, λμ λλ¦¬λ‘ λ³νν΄μ νλ‘κ·Έλ¨μ λ°μνλ κ²μ΄ μΌλ°μ μ΄λ€.
μλλ json νμΌμ open ν loadν¨μλ₯Ό ν΅ν΄ json_dataλ‘ μ½κ³ νμν λ°μ΄ν°λ€μ 리μ€νΈμ λ°λ‘ λ΄μμ countμ λ°μ΄ν° λͺ¨λ μΆλ ₯νλ μμ μ΄λ€.
import json
with open("ETHUSDT.json", "r") as f:
json_data = json.load(f)
# print(json.dumps(json_data))
buy_arr = []
sell_arr = []
for json_obj in json_data:
if "SELL" in json_obj["side"] and ("SEOA" in json_obj["clientOrderId"] or "C2O" in json_obj["clientOrderId"] or "REFILL" in json_obj["clientOrderId"]):
sell_arr.append((json_obj["price"], json_obj["side"], json_obj["clientOrderId"]))
if "BUY" in json_obj["side"] and ("SEOA" in json_obj["clientOrderId"] or "C2O" in json_obj["clientOrderId"] or "REFILL" in json_obj["clientOrderId"]):
buy_arr.append((json_obj["price"], json_obj["side"], json_obj["clientOrderId"]))
sub_arr = []
for json_obj in json_data:
if "SUB" in json_obj["clientOrderId"]:
sub_arr.append((json_obj["price"], json_obj["side"], json_obj["clientOrderId"]))
#buy_jes_arr.sort(key = lambda x:x[0], reverse=True)
print("Total count = ", len(json_data))
print("JES_BUY count = ", len(buy__arr))
print("JES_SELL count = ", len(sell_arr))
print("SUB count = ", len(sub_arr))
print("---- DATA ----")
print("BUY: ", buy_arr)
print("SELL: ", sell_arr)
print("SUB:", sub_arr)
μλλ jsonνμΌμ λ΄μ©μ΄λ€. Array λ΄ κ°μ²΄κ° 200κ°κ° λκΈ° λλ¬Έμ μΌλΆλ§ κ°μ Έμλ€.
[
{
"time": "1684375756476",
"updateTime": "1684375756499",
"orderId": "1423041418184758528",
"clientOrderId": "SELL-755933-1020003-100-O-SEOA-OT",
"symbol": "ETHUSDT",
"price": "1823.35",
"orderType": "LIMIT",
"side": "SELL",
},
{
"time": "1684375756467",
"updateTime": "1684375756769",
"orderId": "1423041418159521024",
"clientOrderId": "BUY-755933-1020003-1-O-SEOA",
"symbol": "ETHUSDT",
"price": "1820.85",
"orderType": "LIMIT",
"side": "BUY",
},
{
"time": "1684375750347",
"updateTime": "1684375753853",
"orderId": "1423041366687094016",
"clientOrderId": "BUY-749778-1020003-1-O-SUB",
"symbol": "ETHUSDT",
"price": "1819.25",
"orderType": "LIMIT",
"side": "BUY",
},
{
"time": "1684375750326",
"updateTime": "1684375750349",
"orderId": "1423041366645079296",
"clientOrderId": "SELL-749778-1020003-100-O-REFILL-OT",
"symbol": "ETHUSDT",
"price": "1819.05",
"orderType": "LIMIT",
"side": "BUY"
}
...
]