How to get a specific string from file? [closed] - python
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a JSON file like this:
{"objects":[{"featureId":"ckm39acfw00043b6a4i8vv8zf","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{
top":110,"left":799,"height":42,"width":53},"instanceURI":<"https://api.labelbox.com/masks/feature/ckm39acfw00043b6a4i8vv8zf?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39agzr00073b6alzzwpm77","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":151,"left":875,"height":45,"width":120},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39agzr00073b6alzzwpm77?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39an0e000a3b6ae7vc0bo8","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":635,"left":952,"height":93,"width":84},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39an0e000a3b6ae7vc0bo8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39bbki000g3b6au6s5s3se","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":646,"left":764,"height":74,"width":93},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39bbki000g3b6au6s5s3se?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cgdi000p3b6aru669fzh","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":375,"left":916,"height":52,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cgdi000p3b6aru669fzh?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39ckyi000s3b6armui3tn3","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":420,"left":914,"height":72,"width":86},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39ckyi000s3b6armui3tn3?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cp6a000v3b6a6cjj16xp","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":478,"left":867,"height":66,"width":137},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cp6a000v3b6a6cjj16xp?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39cyom000y3b6aqp2x5i0s","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":703,"left":806,"height":85,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cyom000y3b6aqp2x5i0s?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39dz3t00143b6a2brbj4qi","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":41,"left":823,"height":50,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39dz3t00143b6a2brbj4qi?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39eco400173b6a35p84q7y","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":31,"left":892,"height":62,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39eco400173b6a35p84q7y?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"}],"classifications":[]}
and I want to save in array all occurrences of top, left, right, height and width. How can I do that?
Firstly, the JSON file does not look valid. For my answer I'll assume a valid file like so:
#example.json
{
"top":151,
"left":875,
"height":45,
"width":120
}
Next you can use python's builtin library 'json' to load the json and extract information:
import json
with open('test.json', 'r') as fh:
d = json.load(fh)
print(d.get('top'))
Notice that first you load the json file's content into a dictionary and then you can access it's contents in the same way you would a dictionary using d.get('top') or d['top']
Related
How can I put all keys of file that contain key value pairs in quotes using python code? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question I have a file contain key value pairs below form: [ { af_NA: "Afrikaans (Namibia)", af_ZA: "Afrikaans (South Africa)", af: "Afrikaans", ak_GH: "Akan (Ghana)", ak: "Akan", sq_AL: "Albanian (Albania)", sq: "Albanian", am_ET: "Amharic (Ethiopia)", ... } ] but keys are not in quotes. I want put all keys in quotes using python code. Thanks
this looks like valid yaml, so you could use PyYaml to parse the file, then dump it to json with open('your_file') as fl: data = yaml.safe_load(fl) print(json.dumps(data))
Is there a way to get all keys that match a string in a json file's values and output them to a text file? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago. Improve this question I'm trying to get all keys' values that equal "url" ignoring nesting from a JSON file and then output them to a text file. How would I go about doing this? I'm running Python 3.7 and cannot seem to find a solution. r = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json') j = r.json() The result expected from this would be a text file filled with links from this json file. https://launchermeta.mojang.com/v1/packages/31fa028661857f2e3d3732d07a6d36ec21d6dbdc/a1.2.3_02.json https://launchermeta.mojang.com/v1/packages/2dbccc4579a4481dc8d72a962d396de044648522/a1.2.3_01.json https://launchermeta.mojang.com/v1/packages/48f077bf27e0a01a0bb2051e0ac17a96693cb730/a1.2.3.json etc.
Using requests library import requests response = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json').json() url_list = [] for result in response['versions']: url_list.append(result['url']) print(url_list)
Extract data from JSONs inside the CSV [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago. Improve this question Could you advise what would be the best way to extract an element from JSON when I have multiple JSONs in a CSV file? The data structure is like this Line1: {"CreationTime":"2018-10-29T13:40:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPaddress":"x.x.x.x"...} Line 2: {"CreationTime":"2018-10-29T13:41:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPAddress":"x.x.x.x"...} ..... LineYY:... My goal is to extract the IPAddress value from every line, that means from every json array. What would be the best way to do it? Thanks a lot!
You can use json to load each line into a dictionary and then access IPAddress import json ips = [] for line in data.readlines(): row = json.loads(line) ips.append(row['IPAddress'])
Formatting Numbers Inside of a String [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago. Improve this question Currently I'm working with an API of an online game I play to build a tool, and in doing so I've encountered a problem. The API returns JSON files to the user. While working on creating a class that parses these JSON files for me I've realized I'd like to be able to format a number inside of one of them so instead of being "585677088.5" it's "585,677,088". This would be easy enough if the string just contained this number however this string contains a bunch of other text as well. Here's a block of the text loan:0 unpaidfees:-3510000 total:585677088.5 I'm using python to do this. The only existing code I have in place is: import urllib2 data = urllib2.urlopen("URL")
Like this: >>> my_str = """loan:0 ... unpaidfees:-3510000 ... total:585677088.5""" >>> map(lambda x: (x.split(":")[0], int(float(x.split(":")[-1]))), my_str.split("\n")) [('loan', 0), ('unpaidfees', -3510000), ('total', 585677088)]
How do I read a text file into a string variable in Python starting at the second line? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 7 years ago. Improve this question I use the following code segment to read a file in python file = open("test.txt", "rb") data=file.readlines()[1:] file.close print data However, I need to read the entire file (apart from the first line) as a string into the variable data. As it is, when my files contents are test test test, my variable contains the list ['testtesttest']. How do I read the file into a string? I am using python 2.7 on Windows 7.
The solution is pretty simple. You just need to use a with ... as construct like this, read from lines 2 onward, and then join the returned list into a string. In this particular instance, I'm using "" as a join delimiter, but you can use whatever you like. with open("/path/to/myfile.txt", "rb") as myfile: data_to_read = "".join(myfile.readlines()[1:]) ... The advantage of using a with ... as construct is that the file is explicitly closed, and you don't need to call myfile.close().