Saving a Dictionary to File While keeping it as a Dictionary - python

itemsInExistence = []
item = {}
item['name'] = input("What do you want the new item to be called? ")
item['stats'] = int(input("What is its stat? "))
item['rank'] = int(input("What is its base rank? "))
item['amount'] = int(input("How many of it are there? "))
for i in range(item['amount']):
itemsInExistence.append(item)
def save_list2():
with open('itemsleft.txt', 'wb') as f:
i = 0
for item in itemsInExistence:
pickle.dump(itemsInExistence, f)
i += 1
I tried to save it both normally and with pickle, but neither keeps the dictionary's values. I need to save the dictionary to the file and retrieve it from the file with 'stats', 'rank', 'amount' still being integers and still separate from the rest of the line. (Keep in mind that there will be more than one saved item in itemsInExistence, both to be saved and loaded.)
def save_list2():
ii = 0
for i in itemsInExistence:
d = itemsInExistence[ii]
json.dump(d, open(files2, 'w'))
ii += 1
def load_list2():
with open(files2,'r') as a:
for line in a:
line = line.strip()
itemsInExistence.append(line)

You may use JSON format to store a dict into a file, it's quite easy
import json
file = "foofile"
d = dict()
# fill d
# save data : format the dict to a string and it into the file
json.dump(d, open(file, 'w'))
# read data : read the file's content and parse to a dict
a = json.load(open(file))

Related

How to save a dictionary as a JSON file?

I have some invoice items:
lista_items = {}
lineNumber = 0
for line in self.invoice_line_ids:
lineNumber = lineNumber + 1
print lineNumber
lista_items["numeroLinea"] = [lineNumber]
lista_items["cantidad"] = [line.quantity]
lista_items["costo_total"] = [line.price_subtotal]
lista_items["precioUnitario"] = [line.price_unit]
lista_items["descripcion"] = [line.name]
# for line_tax in line.invoice_line_tax_ids:
# print line_tax.amount
# print line_tax.id
# # print line.invoice_line_tax_ids
return lista_items
I need to save the items in a dictionary and after that to save it to a JSON.
How can I do it?
You can use json.dump() to save a dictionary to a file. For example:
# note that output.json must already exist at this point
with open('output.json', 'w+') as f:
# this would place the entire output on one line
# use json.dump(lista_items, f, indent=4) to "pretty-print" with four spaces per indent
json.dump(lista_items, f)
In the following code just replace the variable d with your dictionary and put your filename in place of 'json_out'. Take note of the parameter w+, it opens the file both for reading and writing and overwrites the existing file if any.
Also note that there is also 'dumps' method in json which will give you string representation of the dict.
import json
d = {'x':2,'y':1}
out_file = open('json_out','w+')
json.dump(d,out_file)
just dump the lista_items in a json file like:
import json
lista_items = {}
lineNumber = 0
for line in self.invoice_line_ids:
lineNumber = lineNumber + 1
lista_items["numeroLinea"] = [lineNumber]
lista_items["cantidad"] = [line.quantity]
lista_items["costo_total"] = [line.price_subtotal]
lista_items["precioUnitario"] = [line.price_unit]
lista_items["descripcion"] = [line.name]
with open('file.json', 'w') as fp:
json.dump(lista_items, fp, indent=4)

Sorting and sequencing data from a file

I've developed a program that stores a list of ids, so:
But for the desired purposes, the data should take the sequential form, so that the first pair of ids is something like: "889926212541448192" becomes 1 and "889919950248448000" becomes 2. That is, the file to be should be something like:
Where the first id connects with 2,3 and 6, and the id 4 only with 5, forming a network.
I have no experience in this area, but I can not find a way to do this reading.
I tried to do some programs, but they only read row and not column id to id. This data is saved following the following program
import json
arq = open('ids.csv','w')
arq.write('Source'+','+'Target')
arq.write("\n")
lista_rede = [] #list to store all ids
with open('dados_twitter.json', 'r') as f:
for line in f:
lista = []
tweet = json.loads(line) # to write as a Python dictionary
lista = list(tweet.keys()) #write list of keys
try:
if 'retweeted_status' in lista:
id_rt = json.dumps(tweet['retweeted_status']['id_str'])
id_status = json.dumps(tweet['id_str'])
lista_rede.append(tweet['id_str'])
lista_rede.append(tweet['retweeted_status']['id_str'])
arq.write( id_status +','+ id_rt )
arq.write("\n")
if tweet['quoted_status'] in lista :
id_rt = json.dumps(tweet['quoted_status']['id_str'])
id_status = json.dumps(tweet['id_str'])
lista_rede.append(tweet['id_str'])
lista_rede.append(tweet['quoted_status']['id_str'])
arq.write( id_status +','+ id_rt )
arq.write("\n")
except:
continue
arq.close()
As a result I have a file with ids data in pairs of interactions.
How can I then rearrange these data in reading, or even how to write them ?? In Python or another language?
The following snippet would do the job-
import re
header = ''
id_dict = {}
# read the ids
with open('ids.csv') as fr:
header = fr.readline()
for line in fr:
ids = [int(s) for s in re.findall(r'\d+', line)]
try:
id_dict[int(ids[0])].append(int(ids[1]))
except:
id_dict[int(ids[0])] = [int(ids[1])]
# sort the ids
for key in id_dict:
id_dict[key].sort()
# save the sorted ids in a new file
with open('ids_sorted.txt', 'w') as fw:
# fw.write(header)
for key in sorted(id_dict):
for value in id_dict[key]:
fw.write("{0} {1}\n".format(key, value))

how to make the offset drop line while the lines aren't the same length?

def load_from_file():
d = {} # create empty dict
file = open("players.txt", "r")# open file for reading
line = file.readline()
file.close()# we’re done with the file
list = line.split(",")
prop = {"position":"","number":"","name":"","birth":"","id":""}
keys = (sorted(prop))
num = 0
for key in keys:
d[key] = list[num]
num += 1
return d
The problem is that whenever the loop returns to this function it reads the same line! and i want the offset to drop a new line
The problem is that you're telling the program to only read the first line of the file every time you call the function at the file.readline() statement. You should read all the file in at once into a list, then loop through the lines that have been read into the list.
Example:
def load_from_file():
with open("players.txt", "r") as myfile # open file for reading
myfile = file.readlines()
return myfile
def create_dictionary(line):
d = {}
list = line.split(",")
prop = {"position":"","number":"","name":"","birth":"","id":""}
keys = (sorted(prop))
num = 0
for key in keys:
d[key] = list[num]
num += 1
return d
data = []
filedata = load_from_file()
for line in filedata:
data.append(create_dictionary(line))
P.S. Not sure what all you're trying to do with data, but this should help you get the gist.
Using DictReader from the csv module:
def load_from_file():
with open("players.txt") as players:
fields = ["birth", "id", "name", "number", "position"]
reader = csv.DictReader(players, fields)
return list(reader)

How to convert Double dict from file and use it

I have double dict which i want read using json, but i don't really know how. Im running Python 3
{ "www.svetaine.lt":{ "true": "111.111.222.11" }}
{ "www.svetaine2.lt":{ "true": "111.111.222.11" }}
I know only how to make simple dict
def openfile():
data = []
d={}
with open('test.json', 'r') as f:
for line in f:
data.append(json.loads(line))
for item in data:
d.update(item)
f.close()
return d
So I would like to improve my code by using double dict.
P.S Could you also mention how I could mention refer to it after reading like
for ele in d:
getname="www.svetaine.lt"
getstatus="true"
getip= "111.111.222.11"
import json
def openfile():
d={}
with open('s.json', 'r') as f:
for line in f:
line_ = json.loads(line)
name = line_.keys()[0]
status = line_[name].keys()[0]
ip = line_[name][status]
d[name] = {'name':name, 'status':status, 'ip':ip}
f.close()
return d
data = openfile()
# print whole dict
for ele in data:
print ele, data[ele]
# retrieving info w.r.t name
print data['www.svetaine.lt']

writing a csv to file

I need to write the output of the code I have to a file so I can call it later. I need to call the output not the original test1 file. the code I have that makes the output is below and works fine, I just can't get it to a file a can call later.
import csv
file1 = open('C:/Users/Gallatin/Documents/adamenergy.csv',"r") #Open CSV File in Read Mode
reader = csv.reader(file1) #Create reader object which iterates over lines
class Object: #Object to store unique data
def __init__(self, name, produce, amount):
self.name = name
self.produce = produce
self.amount = amount
rownum = 0 #Row Number currently iterating over
list = [] #List to store objects
def checkList(name, produce, amount):
for object in list: #Iterate through list
if object.name == name and object.produce == produce: #Check if name and produce combination exists
object.amount += int(amount) #If it does add to amount variable and break out
return
newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount
list.append(newObject) #Add to list and break out
for row in reader: #Iterate through all the rows
if rownum == 0: #Store header row seperately to not get confused
header = row
else:
name = row[0] #Store name
produce = row[1] #Store produce
amount = row[2] #Store amount
if len(list) == 0: #Default case if list = 0
newObject = Object(name, produce, int(amount))
list.append(newObject)
else: #If not...
checkList(name, produce, amount)
rownum += 1
for each in list:
print each.name,each.produce,each.amount
With the print it generates the output i want correctly, but i need to write this output to a file so I can call it later using ndiff to compare to another csv file I will run through similar code above
There's several approaches you can take:
You can either run the program differently; instead of running:
./generate
run
./generate > output_file.csv
This uses shell redirection to save the standard output to whatever file you specify. This is extremely flexible and very easy to build into future scripts. It's especially awesome if you accept input on standard input or via a named file, it makes your tool extremely flexible.
You can modify your program to write to a specific file. Open the file with something like:
output = open("output.csv", "w")
and then write output using strings along the lines of
output.write(each.name + "," + str(each.produce) + "," + str(each.amount))
You could use the same csv module to also write files. This might be the better approach for long-term use, because it'll automatically handle complicated cases of inputs that include , characters and other difficult cases.
Simply redirect output to the file.
For example,
C:> python myfile.py > output.txt
At the top of the file, open your output file:
outf = open("my_output_file.csv", "wb")
then later:
print >>outf, each.name,each.produce,each.amount
Just use CSV writer
add below code:
csvWriter = csv.writer(open('csvFileSource.csv', 'wb'), delimiter=',', quotechar='|',quoting=csv.QUOTE_MINIMAL)
csvWriter.writerow(each.name + each.produce + each.amount)
full code :
import csv
file1 = open('csvFileSource.csv',"r") #Open CSV File in Read Mode
reader = csv.reader(file1) #Create reader object which iterates over lines
class Object: #Object to store unique data
def __init__(self, name, produce, amount):
self.name = name
self.produce = produce
self.amount = amount
rownum = 0 #Row Number currently iterating over
list = [] #List to store objects
def checkList(name, produce, amount):
for object in list: #Iterate through listif object.name == name and object.produce == produce: #Check if name and produce combination exists
object.amount += int(amount) #If it does add to amount variable and break out
return
newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount
list.append(newObject) #Add to list and break out
for row in reader: #Iterate through all the rows
if rownum == 0: #Store header row seperately to not get confused
header = row
else:
name = row[0] #Store name
produce = row[1] #Store produce
amount = row[2] #Store amount
if len(list) == 0: #Default case if list = 0
newObject = Object(name, produce, int(amount))
list.append(newObject)
else: #If not...
checkList(name, produce, amount)
rownum += 1
csvWriter = csv.writer(open('csvFileDestination.csv', 'wb'), delimiter=',', quotechar='|',quoting=csv.QUOTE_MINIMAL)
for each in list:
csvWriter.writerow(each.name + each.produce + each.amount)

Categories