-2

I need to get the some part of the text from below text with python.

original_text ="{"info":{"satname":"SPACE STATION","satid":00000,"transactionscount":0},"positions":[{"satlatitude":-50.00807313,"satlongitude":-37.47024176,"sataltitude":435.04,"azimuth":178.02,"elevation":-47.22,"ra":132.83647708,"dec":-72.05784906,"timestamp":1589984178},{"satlatitude":-49.98953872,"satlongitude":-33.37828456,"sataltitude":445.03,"azimuth":135.94,"elevation":-47.13,"ra":132.83937425,"dec":-62.01268023,"timestamp":1559981179}]}"

Expected outout is : "{"info":{"satname":"SPACE STATION","satid":00000,"transactionscount":0},"positions":[{"satlatitude":-50.00807313,"satlongitude":-37.47024176,"sataltitude":435.04,"azimuth":178.02,"elevation":-47.22,"ra":132.83647708,"dec":-72.05784906,"timestamp":1589984178}"

My code is:

import re
result = re.match(r'^{\"info\":.+\{', original_text) 
print(result)

I'm getting None as output. This reg patterns woks in regex101.com. What is the mistake I have done

user
  • 145
  • 1
  • 2
  • 12
  • How about just `json.loads`? – ggorlen May 20 '20 at 16:03
  • Sorry, It won't help for my rest of the task, I need to get only first occurrence of position. – user May 20 '20 at 16:06
  • Using `json.loads(s)["positions"][1]` worked fine for me after fixing the invalid number (`00000`). Parsing JSON with a regex is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). In fact, it's used as the canonical example in that link. It's unclear what `original_text` is, anyway. It looks like a dict but I assume it's a string or `re.match` will raise. Can you clarify? If it's a dict, then just use `original_string["positions"][1]`. – ggorlen May 20 '20 at 16:11
  • As you shown to the top `original_text` is not a string so you cannot perform re.match – Mathix420 May 20 '20 at 16:13
  • 1
    Does this answer your question? [How to parse JSON in Python?](https://stackoverflow.com/questions/7771011/how-to-parse-json-in-python) – ggorlen May 20 '20 at 16:13
  • @ggorlen, I did json.loads(s)["positions"][1], but it gave only after position. I need from the beginning until finish of the first position. Pls see my expected results. – user May 20 '20 at 16:23
  • I see. Then just parse it into a dict with `d = json.loads(s)`, then `d["positions"].pop(); print(d)`. You might want to read a bit about manipulating lists and dicts in the docs or in a tutorial. – ggorlen May 20 '20 at 16:28
  • @ggorlen, you saved my life, Thanks a lot – user May 20 '20 at 16:33
  • @user if you have a string, please put quotes around it. Failing to do so causes a lot of confusion. It's a totally different data type than a dictionary, which is what it looks like to people. – ggorlen May 20 '20 at 16:33
  • @user no problem, good luck. – ggorlen May 20 '20 at 16:34
  • @ggorlen, I made the correction, sorry for the confusion, you can add your answer then I can accept and up vote – user May 20 '20 at 16:36
  • I appreciate it. I voted to close the question as a dupe since it basically was a misunderstanding of how to parse a JSON string. – ggorlen May 20 '20 at 16:38

1 Answers1

0

if you want to get a part to text, you can simply access without regex as :

print(original_text["info"])
print(original_text["positions"][0])
print(original_text["positions"][1])