I have a file with multiple dictionaries, one in each line.
They all have the same keys. I want to rename one key from 'id' to 'orderid' in all of them. What is the most efficient way to do so?
Sample data:
{'total_ex_tax': '11.0000', 'is_deleted': False, 'status_id': 5, 'id': 614534}
{'total_ex_tax': '100.0000', 'is_deleted': False, 'status_id': 5, 'id': 614535}
Code so far:
def popAndMergeDicts(dicts):
dictLine = ast.literal_eval(dicts)
tempDict = dictLine['billing_address']
del dictLine['billing_address']
for i in tempDict:
dictLine[i] = tempDict[i]
# insertOrdersInDatabase(dictLine)
rename_id(dictLine)
return dictLine
def rename_id(dictionary):
pass
def process_orders_file(filename):
lines = tuple(open(filename))
for line in lines[0:]:
popAndMergeDicts(line)
process_orders_file('allOrdersData')
This is trivial:
def rename_id(dictionary):
try:
dictionary['new_key'] = dictionary.pop('old_key')
except KeyError:
# 'old_key' is not present in dictionary.
if not dictionary.has_key('new_key'):
raise KeyError('This dictionary is missing "old_key": %s' %
dictionary)
If KeyError is raised, search your file for the error-causing dictionary and correct it as necessary.
Do you want to use the new dict directly, if not then:
with open("input.txt") as f:
for line in f:
print(line.strip().replace("id", "orderid"))
If you want to use it as dict, then you can try:
import ast
with open("input.txt") as f:
for line in f:
mydict = ast.literal_eval(line.strip())
mydict["ordeid"] = mydict.pop("id")
# use mydict
Related
I got a complex nested JSON file and I need to convert it completely, but my code can only convert part of the information,
How can I modify my code, or do you guys have a better code?
my json file
import csv
import json
import sys
import codecs
def trans(path):
jsonData = codecs.open('H://14.json', 'r', 'utf-8')
# csvfile = open(path+'.csv', 'w')
# csvfile = open(path+'.csv', 'wb')
csvfile = open('H://11.csv', 'w', encoding='utf-8', newline='')
writer = csv.writer(csvfile, delimiter=',')
flag = True
for line in jsonData:
dic = json.loads(line)
if flag:
keys = list(dic.keys())
print(keys)
writer.writerow(keys)
flag = False
writer.writerow(list(dic.values()))
jsonData.close()
csvfile.close()
if __name__ == '__main__':
path=str(sys.argv[0])
print(path)
trans(path)
my json file
{"id":"aa","sex":"male","name":[{"Fn":"jeri","Ln":"teri"}],"age":45,"info":[{"address":{"State":"NY","City":"new york"},"start_date":"2001-09","title":{"name":"Doctor","Exp":"head"},"year":"2001","month":"05"}],"other":null,"Hobby":[{"smoking":null,"gamble":null}],"connect":[{"phone":"123456789","email":"info#gmail.com"}],"Education":"MBA","School":{"State":"NY","City":"new york"}}
{"id":"aa","sex":"female","name":[{"Fn":"lo","Ln":"li"}],"age":34,"info":[{"address":{"State":"NY","City":"new york"},"start_date":"2008-11","title":{"name":"Doctor","Exp":"hand"},"year":"2008","month":"02"}],"other":null,"Hobby":[{"smoking":null,"gamble":null}],"connect":[{"phone":"123456789","email":"info#gmail.com"}],"Education":"MBA","School":{"State":"NY","City":"new york"}}
It only converts part of the information, 'name''info''Hobby''connect''School' these information are not converted,I need to convert all information completely,
You could use the below function to treat each dic. It will flatten the dict by recursive calls until there is no dict or list in the values. In order to avoid issues with 2 keys having the same name, I concatenate with the previous level.
WARNING: it is based on your format so if you have lists with more than one element in the middle, it will only take care of the first element.
def flatten_dict(input_dict, result = None):
result = result or {}
for key, value in input_dict.items():
if isinstance(value, list):
current_dict = {key+"_"+k: v for k, v in value[0].items()}
flatten_dict(current_dict, result)
elif isinstance(value, dict):
current_dict = {key+"_"+k: v for k, v in value.items()}
flatten_dict(current_dict, result)
else:
result[key] = value
return result
Then apply this function on each dic, transform to Dataframe and save as CSV.
res = []
for line in jsonData:
dic = json.loads(line)
res.append(flatten_dict(dic))
res_df = pd.DataFrame(res)
res_df.to_csv("result.csv")
Result:
here is my txt file that has contained all of the lines. What I want to do is create a dictionary, and access a key, and get a list of values
Finance:JPMC
Software:Microsoft
Conglomerate:L&T
Conglomerate:Amazon
Software:Palantir
Defense:BAE
Defense:Lockheed
Software:TCS
Retail:TjMax
Retail:Target
Oil:Exxon
Oil:Chevron
Oil:BP
Oil:Gulf
Finance:Square
FMCG:PnG
FMCG:JohnsonNJohnson
FMCG:Nestle
Retail:Sears
Retail:FiveBelow
Defense:Boeing
Finance:Citadel
Finance:BridgeWater
Conglomerate:GE
Conglomerate:HoneyWell
Oil:ONGC
FMCG:Unilever
Semiconductor:Intel
Semiconductor:Nvidia
Semiconductor:Qualcomm
Semiconductor:Microchip
Conglomerate:Samsung
Conglomerate:LG
Finance:BoA
Finance:Discover
Software:TCS
Defense:Raytheon
Semiconductor:Microsemi
Defense:BAE
Software:Meta
Oil:SinoPec
Defense:Saab
Defense:Dassault
Defense:Airbus
Software:Adobe
Semiconductor:TSMC
FMCG:CocoCola
FMCG:Pesico
Retail:Kohls
Here is my attempted code
f = open("companyList.txt", "r")
sector, company = [], []
for line in f:
first, second = line.split(":")
sector.append(first)
company.append(second)
dictionary = {}
for key in sector:
for element in company:
dictionary[sector].append(element)
print(dictionary)
Since there are multiple duplicate keys, I wanted to append a list to that particular key as python doesn't allow duplicate keys.
If i understand your question right you can do this:
from collections import defaultdict
dictionary = defaultdict(list)
for line in f:
first, second = line.split(":")
dictionary[first].append(second)
I think this is what you want:
pairs = {}
with open("tst.txt", "r") as f:
while True:
line = f.readline().strip()
if not line:
break
sector, value = line.split(":", 1)
if sector not in pairs:
pairs[sector] = []
pairs[sector].append(value)
f.close()
print(pairs)
you should do:
f = open("companyList.txt", "r")
sector, company = [], []
for line in f:
first, second = line.split(":")
sector.append(first)
company.append(second)
dictionary = {}
for sectory,companyy in zip(sector,company):
dictionary[sectory] = companyy
for key in sector:
dictionary[sector] = key
So far I have this code which is creating a dictionary from an input file:
def read_file(filename):
with open("menu1.csv") as file:
file.readline()
for line in file:
line_strip = [line.rstrip('\n')]
lines= [line.split(',')]
result = {key: (float(fl), int(intg),
text.strip()) for key,
fl, intg,text in lines}
print(result)
read_file("menu1.csv")
I have to keep that code in that def format. However, this outputs 27 different dictionaries. How do I make it so it is all in ONE dictionary?
ALso:
I want to alphabetize the keys and put them into a list. I tried something like this but it won't work:
def alphabetical_menu(dict):
names = []
for name in d:
names.append(name)
names.sort()
print(names)
What am I doing wrong? or do you have a way to do it?
Is this what you wanted?
def read_file(filename):
result = {}
with open(filename) as file:
file.readline()
for line in file:
line_strip = line.rstrip()
line_split= line.split(',')
key, fl, intg, text = tuple(line_split)
result[key] = (float(fl), int(intg), text.strip())
return result
def alphabetical_menu(d):
return sorted(d.keys())
menu_dict = read_file("menu1.csv")
menu_sorted_keys = alphabetical_menu(menu_dict)
# To check the result
print(menu_dict)
print(menu_sorted_keys)
I am trying to figure out to store content of the file into multiple values in the specific key.
Desired output:
{'city1':[Island-1,Island-3],'city2':[Island-2,Island-4]}
data.txt
city1-south:"London"
city1-south:"Paris"
city1-north:"Amsterdam"
city1-north:"Island-1"
city2-south:"Island-2"
city1-east:"Island-3"
city2-west:"Island-4"
def readFile(data_file):
data = open(data_file,"r")
d = {}
for line in data:
if 'Island' in line:
city,loc = line.rstrip("\n").split(":",1)
d[city] = loc
print (d)
data.close()
data_file = "data.txt"
readFile(data_file)
Current output:
{'city2-south': '"Island-2"', 'city2-west': '"Island-4"', 'city1-east': '"Island-3"', 'city1-north': '"Island-1"'}
I can not run your code now because config_file is not defined. I have made some modifications so that your code can run.
with open("data.txt") as data:
d = {'city1': [], 'city2': []}
for line in data:
if 'Island' in line:
city,loc = line.rstrip("\n").split(":",1)
for key in d.keys():
if key in city:
d[key].append(loc[1:-1])
print(d)
Results:
{'city1': ['Island-1', 'Island-3'], 'city2': ['Island-2', 'Island-4']}
For now Island-1 etc. can noly be output as string in the dictionary since otherwise python would treat them as variables.
This is the data I have in my data.txt file
{"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0},
{"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
How would I be able to get only data from every setup in the
dictionaries using a loop?
Thanks
I'm not sure how you're getting the dictionaries into your text file in the first place, but if it's possible to drop the trailing commas, i.e.
{"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0}
{"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
Something like this may work for you:
def dicts_from_file(file):
dicts_from_file = []
with open(file,'r') as inf:
for line in inf:
dicts_from_file.append(eval(line))
return dicts_from_file
def get_setups(dicts):
setups = []
for dict in dicts:
for key in dict:
if key == "setup":
setups.append(dict[key])
return setups
print get_setups(dicts_from_file("data.txt"))
f = open('data')
for line in f:
d = ast.literal_eval(line)[0]
print d['setup']
for this code you need to put ',' after every line because ast.literal_eval(line) convert line into a tuple.
and if you do not have ',' after every dict then use this
f = open('data')
for line in f:
d = ast.literal_eval(line)
print d['setup']
You can try this if the line in your file is standard dict string.
def get_setup_from_file(file_name):
result = []
f = open(file_name, "r")
for line in f.xreadlines():
# or line_dict = json.loads(line)
line_dict = eval(line) # if line end witch ',', try eval(line[0:-1])
result.append(line_dict["setup"])
return result
Wish this can help you.
if it is standard dict string, try this:
with open(file,'r') as file_input:
for line in file_input:
print eval(line).get("setup")