in Education by
Here is my sample string: [true, {"name": "NameofItem", "amount": "1", "price": 100, "sellerName": "Sellername1", "sellerId": 1}, {"name": "NameofItem2", "amount": "1", "price": 101, "sellerName": "Sellername2", "sellerId": 2}, 22250] I need to find a way of grabbing the first instance of "price" and it's affiliated integer. I thought I could use re.match and (\{.*?\}) to get a first set of content and then convert to a dictionary, but I can't seem to make that work. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Don't use a regex to parse well-formed JSON like this. Use the builtin json.loads function to parse the JSON string into an in-memory list data structure. Then you can access the first element in the list with L[1] and the "price" key in that dictionary with L[1]["price"]: >>> s = '[true,{"name":"NameofItem","amount":"1","price":100,"sellerName":"Sellername1", "sellerId":1},{"name":"NameofItem2","amount":"1","price":101,"sellerName":"Sellername2","sellerId":2},22250]' >>> import json >>> L = json.loads(s) >>> L [True, {'name': 'NameofItem', 'amount': '1', 'price': 100, 'sellerName': 'Sellername1', 'sellerId': 1}, {'name': 'NameofItem2', 'amount': '1', 'price': 101, 'sellerName': 'Sellername2', 'sellerId': 2}, 22250] >>> L[1]["price"] 100 >>> type(L[1]["price"]) Learn Python for Data Science Course to improve your technical knowledge.

Related questions

0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    When I run pip3 install -r requirements.txt on the project I get this error message: pip._vendor.pkg_resources. ... on my machine. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I know a little of Python and more than a year ago I wrote a small script, using pipenv to manage the ... 3.0 should work. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am starting to work with python again after 8 years. I am trying to do the program with BeautifulSoup and an ... '__main__': main() Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    webbrowser.open('https://api.WhatsApp.com/send?phone=number') I want to send WhatsApp messages to numbers without ... in this link. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Do I need coding skills for Data Science using Python? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    I'm looking for a decent implementation of the OPTICS algorithm in Python. I will use it to form density-based ... to that cluster. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is better than R for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is used in Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is necessary for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...