I want to write this below code in one line. I have lots of data. so the page goes on. So i want to shrink it. How to make it possible. I know it is possible in python. Help me with some solutions.
data['url']=url
data['user agent']=userAgent
data['browser']=browser
data['uniqueId']=uniqueId
data['ip']=ip
data['language']=language
and its going on.
I tried this but it fails.
data['url','user agent','browser'...] = url,useragent,browser....
keys = ("url", "ip", "language")
values = ("http://example.com", "93.184.216.34", "en")
# if you want to update an existing dict:
data = {}
data.update(zip(keys, values))
# if you just want to create a dict:
data = dict(zip(keys, values))
If you want to set all the values at once, you could do something like this:
data = { 'url': url, 'user agent': userAgent, ... }
If data already has... data, you could update it with:
data.update({ 'url': url, 'user agent': userAgent, ... })
You can use dict comprehension:
keys = ['url','user agent','browser']
vals = [url,useragent,browser]
data = {key:val for key,val in zip(keys,vals)}
You could do a for loop:
Example:
for key, value in Data:
finalData[key] = value
Related
I am importing a jsonl file from my hard drive and trying to get it into a usable format. Here is how I'm importing the data.
train_data=[]
with open("Documents/data/train.jsonl",'r',encoding='utf-8') as j:
for line in j:
train_data.append(json.loads(line))
Which produces data structured like this.
train_data[1]
Out[59]:
{'id': 46971,
'img': 'img/46971.png',
'label': 1,
'text': 'text'}
Basically I would like to convert this data to a dictionary format where the dictionary value is the "id" and the rest of the data is associated with that dictionary label. I believe something like the following, but I'm pretty new to Python so I may be displaying this incorrectly.
print(dict_ex)
{46971: ['img/46971.png', 1, 'text']}
You can create a dictionary and add new elements from train_data list one by one:
di = dict()
for o in train_data:
di[o['id']] = [o['img'], o['label'], o['text']]
print(di)
>>> {46971: ['img/46971.png', 1, 'text']}
# dict[key] = value
dict_ex[data['id']] = [data['img'], data['label'], data['text']]
Try this,
result = {}
for d in train_data:
for k, v in d.items():
if k == "id":
result[v] = []
else:
result[v].append(v)
I need to append dictionary values to an already existing JSON file. How can I be able to do that?
My details.json File
{"name": "someName"}
Dictionary generated by my python script
list1 = {"name": "someOthername"}
with open("details.json") as r:
data = json.load(r)
desirableDict = data.append(list1) # It has to be something like this
print(desirableDict)
Desirable Output: {"name": ["someName", "someOthername"]}
You can check all keys within a for loop and put the values of the json file and list1 inside a list like this:
import json
list1 = {"name": "someOthername"}
with open("details.json") as file:
data = json.load(file)
desirableDict = data.copy()
for key in data:
if key in list1:
if type(data[key]) is list:
data[key].append(list1[key])
else:
desirableDict[key] = [data[key],list1[key]]
print(desirableDict)
It seems like you need deep merging of structures. I would like to recommend you to use this awesome library https://pypi.org/project/deepmerge/.
There are a lot of examples like you want to achieve.
from deepmerge import always_merger
base = {"foo": ["bar"]}
next = {"foo": ["baz"]}
expected_result = {'foo': ['bar', 'baz']}
result = always_merger.merge(base, next)
assert expected_result == result
I want to replace keys of the dictionary present in python list using python only.
I have this
Actual Data
[{'Title.1':'Replace key','number':'Replacing
Keys','Title.1':'Replaced Keys'}]
Expected result
[{'Title':'Replace key','number':'Replacing Keys','Title':'Replaced
Keys'}] In place of Title.1 I want Title
data = [{'Title.1':'Replace key','number':'Replacing
Keys','Title.1':'Replaced Keys'}]
for name, datalist in data.iteritems():
for datadict in datalist:
for key, value in datadict.items():
if key == Title.1:
datadict[key] = Title
print (data)
Basicly if you want to change a key you need to create a new key with the desired name copy the data of the old key in it then remove the old key so for that you can use this code :
my_dictionnary = { "old_key": "my_value" }
my_dictionnary["new_key"] = my_dictionnary["old_key"]
del my_dictionnary["old_key"]
So in order to answer to the specific case that you show us you can do something like this :
datas = [{'Title.1':'Replace key','number':'Replacing
Keys','Title.1':'Replaced Keys'}]
for data in datas:
for key in data:
if key[-2:] == ".1":
data[key[:-2]] = data[key]
del data[key]
Here same key given two times ...dictionary keys must be unique
for i in data:
for k,v in i.items():
if k=='Title.1':
del i[k]
i['Title'] = v
print(data)
#[{'number': 'Replacing Keys', 'Title': 'Replaced Keys'}]
I'm running through an excel file reading line by line to create dictionaries and append them to a list, so I have a list like:
myList = []
and a dictionary in this format:
dictionary = {'name': 'John', 'code': 'code1', 'date': [123,456]}
so I do this: myList.append(dictionary), so far so good. Now I'll go into the next line where I have a pretty similar dictionary:
dictionary_two = {'name': 'John', 'code': 'code1', 'date': [789]}
I'd like to check if I already have a dictionary with 'name' = 'John' in myList so I check it with this function:
def checkGuy(dude_name):
return any(d['name'] == dude_name for d in myList)
Currently I'm writing this function to add the guys to the list:
def addGuy(row_info):
if not checkGuy(row_info[1]):
myList.append({'name':row_info[1],'code':row_info[0],'date':[row_info[2]]})
else:
#HELP HERE
in this else I'd like to dict.update(updated_dict) but I don't know how to get the dictionary here.
Could someone help so dictionary appends the values of dictionary_two?
I would modify checkGuy to something like:
def findGuy(dude_name):
for d in myList:
if d['name'] == dude_name:
return d
else:
return None # or use pass
And then do:
def addGuy(row_info):
guy = findGuy(row_info[1])
if guy is None:
myList.append({'name':row_info[1],'code':row_info[0],'date':[row_info[2]]})
else:
guy.update(updated_dict)
This answer suggestion is pasted on the comments where it was suggested that if "name" is the only criteria to search on then it could be used as a key in a dictionary instead of using a list.
master = {"John" : {'code': 'code1', 'date': [123,456]}}
def addGuy(row_info):
key = row_info[1]
code = row_info[0]
date = row_info[2]
if master.get(key):
master.get(key).update({"code": code, "date": date})
else:
master[key] = {"code": code, "date": date}
If you dict.update the existing data each time you see a repeated name, your code can be reduced to a dict of dicts right where you read the file. Calling update on existing dicts with the same keys is going to overwrite the values leaving you with the last occurrence so even if you had multiple "John" dicts they would all contain the exact same data by the end.
def read_file():
results = {name: {"code": code, "date": date}
for code, name, date in how_you_read_into_rows}
If you actually think that the values get appended somehow, you are wrong. If you wanted to do that you would need a very different approach. If you actually want to gather the dates and codes per user then use a defauldict appending the code,date pair to a list with the name as the key:
from collections import defaultdict
d = defaultdict(list)
def read_file():
for code, name, date in how_you_read_into_rows:
d["name"].append([code, date])
Or some variation depending on what you want the final output to look like.
I am incredibly new to python.
I have an array full of json objects. Some of the json objects contain duplicated values. The array looks like this:
[{"id":"1","name":"Paul","age":"21"},
{"id":"2","name":"Peter","age":"22"},
{"id":"3","name":"Paul","age":"23"}]
What I am trying to do is to remove an item if the name is the same as another json object, and leave the first one in the array.
So in this case I should be left with
[{"id":"1"."name":"Paul","age":"21"},
{"id":"2","name":"Peter","age":"22"}]
The code I currently have can be seen below and is largely based on this answer:
import json
ds = json.loads('python.json') #this file contains the json
unique_stuff = { each['name'] : each for each in ds }.values()
all_ids = [ each['name'] for each in ds ]
unique_stuff = [ ds[ all_ids.index(text) ] for text in set(texts) ]
print unique_stuff
I am not even sure that this line is working ds = json.loads('python.json') #this file contains the json as when I try and print ds nothing shows up in the console.
You might have overdone in your approach. I might tend to rewrite the list as a dictionary with "name" as a key and then fetch the values
ds = [{"id":"1","name":"Paul","age":"21"},
{"id":"2","name":"Peter","age":"22"},
{"id":"3","name":"Paul","age":"23"}]
{elem["name"]:elem for elem in ds}.values()
Out[2]:
[{'age': '23', 'id': '3', 'name': 'Paul'},
{'age': '22', 'id': '2', 'name': 'Peter'}]
Off-course the items within the dictionary and the list may not be ordered, but I do not see much of a concern. If it is, let us know and we can think over it.
If you need to keep the first instance of "Paul" in your data a dictionary comprehension gives you the opposite result.
A simple solution could be as following
new = []
seen = set()
for record in old:
name = record['name']
if name not in seen:
seen.add(name)
new.append(record)
del seen
First of all, your json snippet has invalid format - there are dot instead of commas separating some keys.
You can solve your problem using a dictionary with names as keys:
import json
with open('python.json') as fp:
ds = json.load(fp) #this file contains the json
mem = {}
for record in ds:
name = record["name"]
if name not in mem:
mem[name] = record
print mem.values()