簡體   English   中英

無法使用 python 腳本從文本文件解析 JSON 數據

[英]Not able to parse JSON data from text file using python script

我有一個“.txt”文件,其中包含如下所示的 JSON 數據。

[{'tradable': True, 'mode': 'full', 'instrument_token': 4708097, 'last_price': 178.65, 'last_traded_quantity': 5, 'average_traded_price': 180.1, 'volume_traded': 4581928, 'total_buy_quantity': 1282853, 'total_sell_quantity': 1673842, 'ohlc': {'open': 181.95, 'high': 181.95, 'low': 177.8, 'close': 181.0}, 'change': -1.2983425414364609, 'last_trade_time': datetime.datetime(2023, 1, 12, 13, 4, 58), 'oi': 0, 'oi_day_high': 0, 'oi_day_low': 0, 'exchange_timestamp': datetime.datetime(2023, 1, 12, 13, 5, 1), 'depth': {'buy': [{'quantity': 653, 'price': 178.6, 'orders': 8}, {'quantity': 2408, 'price': 178.55, 'orders': 15}, {'quantity': 6329, 'price': 178.5, 'orders': 22}, {'quantity': 9161, 'price': 178.45, 'orders': 24}, {'quantity': 7775, 'price': 178.4, 'orders': 17}], 'sell': [{'quantity': 5726, 'price': 178.7, 'orders': 8}, {'quantity': 4099, 'price': 178.75, 'orders': 11}, {'quantity': 23951, 'price': 178.8, 'orders': 25}, {'quantity': 7446, 'price': 178.85, 'orders': 21}, {'quantity': 11379, 'price': 178.9, 'orders': 21}]}}, {'tradable': True, 'mode': 'full', 'instrument_token': 871681, 'last_price': 972.55, 'last_traded_quantity': 1, 'average_traded_price': 973.85, 'volume_traded': 411290, 'total_buy_quantity': 152925, 'total_sell_quantity': 214765, 'ohlc': {'open': 971.75, 'high': 978.6, 'low': 969.0, 'close': 967.75}, 'change': 0.4959958667011061, 'last_trade_time': datetime.datetime(2023, 1, 12, 13, 4, 53), 'oi': 0, 'oi_day_high': 0, 'oi_day_low': 0, 'exchange_timestamp': datetime.datetime(2023, 1, 12, 13, 5, 4), 'depth': {'buy': [{'quantity': 6, 'price': 972.15, 'orders': 2}, {'quantity': 3, 'price': 972.1, 'orders': 2}, {'quantity': 15, 'price': 972.05, 'orders': 3}, {'quantity': 455, 'price': 972.0, 'orders': 16}, {'quantity': 14, 'price': 971.95, 'orders': 2}], 'sell': [{'quantity': 6, 'price': 972.5, 'orders': 3}, {'quantity': 49, 'price': 972.55, 'orders': 2}, {'quantity': 10, 'price': 972.6, 'orders': 1}, {'quantity': 27, 'price': 972.65, 'orders': 2}, {'quantity': 10, 'price': 972.7, 'orders': 1}]}}]

此數據在從 zerodha websocket 收到后被寫入 a.txt 文件。現在,我想使用我的 python 腳本從 .txt 文件讀取數據,並希望將其加載為 json。但是json.loads() python 中的方法拋出以下錯誤。 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

我也在 python 中嘗試了evalast.literal_eval方法,但它並沒有解決我的問題。 我想要的只是能夠將上述數據作為 JSON 讀取到我的 python 腳本中。 任何線索都會有很大幫助。

讓我從這是一個壞主意開始。

對該問題的評論指出,這不是文件中的 JSON object,而是其他一些 Python 進程打印結果並將其放入文件中的結果。 正確的解決方案是修改生產者以改為使用json.dumps()

除此之外,還有一種將該源文件讀入 Python object 的危險方法。

import datetime  # needed for `eval()`

with open('textfile.txt', 'r') as f:
    data = eval(f.read())

from pprint import pprint
pprint(data)

這將從該輸入產生以下 output:

[{'average_traded_price': 180.1,
  'change': -1.2983425414364609,
  'depth': {'buy': [{'orders': 8, 'price': 178.6, 'quantity': 653},
                    {'orders': 15, 'price': 178.55, 'quantity': 2408},
                    {'orders': 22, 'price': 178.5, 'quantity': 6329},
                    {'orders': 24, 'price': 178.45, 'quantity': 9161},
                    {'orders': 17, 'price': 178.4, 'quantity': 7775}],
            'sell': [{'orders': 8, 'price': 178.7, 'quantity': 5726},
                     {'orders': 11, 'price': 178.75, 'quantity': 4099},
                     {'orders': 25, 'price': 178.8, 'quantity': 23951},
                     {'orders': 21, 'price': 178.85, 'quantity': 7446},
                     {'orders': 21, 'price': 178.9, 'quantity': 11379}]},
  'exchange_timestamp': datetime.datetime(2023, 1, 12, 13, 5, 1),
  'instrument_token': 4708097,
  'last_price': 178.65,
  'last_trade_time': datetime.datetime(2023, 1, 12, 13, 4, 58),
  'last_traded_quantity': 5,
  'mode': 'full',
  'ohlc': {'close': 181.0, 'high': 181.95, 'low': 177.8, 'open': 181.95},
  'oi': 0,
  'oi_day_high': 0,
  'oi_day_low': 0,
  'total_buy_quantity': 1282853,
  'total_sell_quantity': 1673842,
  'tradable': True,
  'volume_traded': 4581928},
 {'average_traded_price': 973.85,
  'change': 0.4959958667011061,
  'depth': {'buy': [{'orders': 2, 'price': 972.15, 'quantity': 6},
                    {'orders': 2, 'price': 972.1, 'quantity': 3},
                    {'orders': 3, 'price': 972.05, 'quantity': 15},
                    {'orders': 16, 'price': 972.0, 'quantity': 455},
                    {'orders': 2, 'price': 971.95, 'quantity': 14}],
            'sell': [{'orders': 3, 'price': 972.5, 'quantity': 6},
                     {'orders': 2, 'price': 972.55, 'quantity': 49},
                     {'orders': 1, 'price': 972.6, 'quantity': 10},
                     {'orders': 2, 'price': 972.65, 'quantity': 27},
                     {'orders': 1, 'price': 972.7, 'quantity': 10}]},
  'exchange_timestamp': datetime.datetime(2023, 1, 12, 13, 5, 4),
  'instrument_token': 871681,
  'last_price': 972.55,
  'last_trade_time': datetime.datetime(2023, 1, 12, 13, 4, 53),
  'last_traded_quantity': 1,
  'mode': 'full',
  'ohlc': {'close': 967.75, 'high': 978.6, 'low': 969.0, 'open': 971.75},
  'oi': 0,
  'oi_day_high': 0,
  'oi_day_low': 0,
  'total_buy_quantity': 152925,
  'total_sell_quantity': 214765,
  'tradable': True,
  'volume_traded': 411290}]

再一次,我要重申,這是一個壞主意

在此處閱讀有關eval細節的更多信息: https://realpython.com/python-eval-function/

您的 JSON 無效。 您可以使用像這樣的在線 JSON 驗證器來檢查: https://jsonformatter.org/

輸入“JSON”后,您可以看到它不是有效格式。 您可以將其正確導出(我強烈推薦),也可以替換錯誤的字符。

目前,您遇到三個問題:

  1. 您使用的是單引號而不是雙引號
  2. 你沒有解析你的日期時間 object,看起來你只是插入了 object,你必須序列化它
  3. 您將值寫為True ,但這是 python 方式而不是 JSON 方式。 您要么必須將其寫為true ,要么必須將其作為字符串傳遞。 我會推薦第一個。

它可能看起來像這樣(但我沒有正確解析日期時間,我只是將其字符串化):

[{"tradable": true, "mode": "full", "instrument_token": 4708097, "last_price": 178.65, "last_traded_quantity": 5, "average_traded_price": 180.1, "volume_traded": 4581928, "total_buy_quantity": 1282853, "total_sell_quantity": 1673842, "ohlc": {"open": 181.95, "high": 181.95, "low": 177.8, "close": 181.0}, "change": -1.2983425414364609, "last_trade_time": "datetime.datetime(2023, 1, 12, 13, 4, 58)", "oi": 0, "oi_day_high": 0, "oi_day_low": 0, "exchange_timestamp": "datetime.datetime(2023, 1, 12, 13, 5, 1)", "depth": {"buy": [{"quantity": 653, "price": 178.6, "orders": 8}, {"quantity": 2408, "price": 178.55, "orders": 15}, {"quantity": 6329, "price": 178.5, "orders": 22}, {"quantity": 9161, "price": 178.45, "orders": 24}, {"quantity": 7775, "price": 178.4, "orders": 17}], "sell": [{"quantity": 5726, "price": 178.7, "orders": 8}, {"quantity": 4099, "price": 178.75, "orders": 11}, {"quantity": 23951, "price": 178.8, "orders": 25}, {"quantity": 7446, "price": 178.85, "orders": 21}, {"quantity": 11379, "price": 178.9, "orders": 21}]}}, {"tradable": true, "mode": "full", "instrument_token": 871681, "last_price": 972.55, "last_traded_quantity": 1, "average_traded_price": 973.85, "volume_traded": 411290, "total_buy_quantity": 152925, "total_sell_quantity": 214765, "ohlc": {"open": 971.75, "high": 978.6, "low": 969.0, "close": 967.75}, "change": 0.4959958667011061, "last_trade_time": "datetime.datetime(2023, 1, 12, 13, 4, 53)", "oi": 0, "oi_day_high": 0, "oi_day_low": 0, "exchange_timestamp": "datetime.datetime(2023, 1, 12, 13, 5, 4)", "depth": {"buy": [{"quantity": 6, "price": 972.15, "orders": 2}, {"quantity": 3, "price": 972.1, "orders": 2}, {"quantity": 15, "price": 972.05, "orders": 3}, {"quantity": 455, "price": 972.0, "orders": 16}, {"quantity": 14, "price": 971.95, "orders": 2}], "sell": [{"quantity": 6, "price": 972.5, "orders": 3}, {"quantity": 49, "price": 972.55, "orders": 2}, {"quantity": 10, "price": 972.6, "orders": 1}, {"quantity": 27, "price": 972.65, "orders": 2}, {"quantity": 10, "price": 972.7, "orders": 1}]}}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM