python - Parse JSON find value and append random number? -
i have json file login , integer valuer (some result), this:
[ { "name":"tamara", "results":"434.545.234.664" }, { "name":"ted", "results":"434.545.234.664" } ]
i need receive user login (name), find inserted name in json. if exists, add number "results".
for example: if input "ted", number appended ted's results this: "results":"434.545.234.664+4343}"
if name doesn't exist, add new record with:
{ "name":"new_name", "results":"some_number" }
in it.
my code, didn't work:
with open('/users/users_db.json') jsonfile: user_name = '' while user_name == '': data = json.load(jsonfile) user_name = input('your name: ') user in data: if user_name in user['name']: print('old user') break else: print('new user')
here's 1 of zillion possible ways code problem:
import json import random import names random.seed(1) data = [ { "name": "tamara", "results": "434.545.234.664" }, { "name": "ted", "results": "434.545.234.664" } ] def foo(lst, name): some_number = random.randint(0, 4343) values = filter(lambda d: d["name"] == name, lst) if values: v in values: v["results"] += "+{0}".format(some_number) else: lst.append({ "name": name, "results": some_number }) name in ["tamara", "ted"] + [names.get_first_name() in range(8)]: foo(data, name) print(data)
this 1 use names module generate random test names.
one advice though, take time read page, sections named "what topics can ask here?" , "what types of questions should avoid asking?". , more importantly, please read the stack overflow question checklist. might want learn minimal, complete, , verifiable examples
Comments
Post a Comment