简体   繁体   English

无法使用 python 脚本从文本文件解析 JSON 数据

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

I have a '.txt' file that contains JSON data like below.我有一个“.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}]}}]

This data was written to a.txt file after it was recieved from zerodha websocket. Now, I want to read the data from the.txt file using my python script and want to load it as a json. But the json.loads() method in python throws the below error.此数据在从 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)

I have tried eval and ast.literal_eval methods in python as well but it didn't solve my problem.我也在 python 中尝试了evalast.literal_eval方法,但它并没有解决我的问题。 All I want is to be able to read the above data as a JSON to my python script.我想要的只是能够将上述数据作为 JSON 读取到我的 python 脚本中。 Any leads would be of great help.任何线索都会有很大帮助。

Let me start off with THIS IS A BAD IDEA .让我从这是一个坏主意开始。

The comments on the question call out that this is not a JSON object in a file, rather the result of some other Python process printing the result and putting that in a file.对该问题的评论指出,这不是文件中的 JSON object,而是其他一些 Python 进程打印结果并将其放入文件中的结果。 The correct solution would be to modify the producer to use json.dumps() instead.正确的解决方案是修改生产者以改为使用json.dumps()

That aside, here's a DANGEROUS way to read that source file into a Python object.除此之外,还有一种将该源文件读入 Python object 的危险方法。

import datetime  # needed for `eval()`

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

from pprint import pprint
pprint(data)

This will produce the following output from that input:这将从该输入产生以下 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}]

Again, I will restate, THIS IS A BAD IDEA .再一次,我要重申,这是一个坏主意

Read more on the specifics of eval here: https://realpython.com/python-eval-function/在此处阅读有关eval细节的更多信息: https://realpython.com/python-eval-function/

Your JSON is not valid.您的 JSON 无效。 You can check that by using an online JSON validator like this one: https://jsonformatter.org/您可以使用像这样的在线 JSON 验证器来检查: https://jsonformatter.org/

After entering the “JSON”, you can see it is not in a valid format.输入“JSON”后,您可以看到它不是有效格式。 You could either export it right, which I would highly recommend, or you could replace the wrong chars.您可以将其正确导出(我强烈推荐),也可以替换错误的字符。

Currently, you have three issues:目前,您遇到三个问题:

  1. You are using single quotes instead of double quotes您使用的是单引号而不是双引号
  2. You are not parsing your datetime object, it looks like you just insert the object, you have to serialize it你没有解析你的日期时间 object,看起来你只是插入了 object,你必须序列化它
  3. You are writing true values as True , but that is the python way and not the JSON way.您将值写为True ,但这是 python 方式而不是 JSON 方式。 You either have to write it as true , or you have to pass it as a string.您要么必须将其写为true ,要么必须将其作为字符串传递。 I would recommend the first one.我会推荐第一个。

It could look like this (but I didnt parse datetime right, I just stringified it):它可能看起来像这样(但我没有正确解析日期时间,我只是将其字符串化):

[{"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