e.g.
I have a dict.
>>> gtf['mykey1']
{'name': {'apple': '20', 'eat': ['Leo', 'Amy', 'Lily', 'Lucy']}
I want to save this output to a local file named out.txt.
How should I do this?
I tried
%store gtf['mykey1'] > out.txt
which did not work.
Thanks.
Did you try just to open a file and write to it:
import json
output = open('output.txt', 'w')
stringified_entry = json.dumps(gtf['mykey1'])
output.write(stringified_entry)
output.close()
Following also works but I find it less convenient to use in a prompt:
with open('output.txt') as output:
output.write(gtf['mykey1'])
Try this:
with open("output.txt","w+") as file:
file.write(gtf['mykey1'])
You need to stringify a dict to save it to a txt in python, you CANNOT save a dict as is, without first making it either a string to save it into .txt format, or json.dump(dict_) it to save it as a json.
This answers your question:
import json
stringified_json = json.dumps(gtf['mykey1'])
output = open('output.txt', 'w')
output.write(stringified_json)
output.close()
json.dumps takes a dict and makes it a string.
You can later load it back to a json with:
import json
output = open('output.txt', 'r')
stringified_json = output.read()
stringified_json = json.loads(stringified_json)
output.close()
Related
I have a txt file that is of the following format (dictionary format):
{'EX1':'No Information Stored',
'EX2':'Foundation',
'EX3':'Foundation',
'EX4':'No Information Stored'}
Does anyone know how I would go about reading this into python to be able to use it like the dictionary that it is?
import json
with open('file.txt', 'r') as w:
data = w.read()
data_as_dict = json.loads(data)
Text file with this structure are JSON, so you can use the json module.
import json
def load_file(filename):
with open(filename) as f:
data = json.load(f)
return data
This is a custom function that return the dictionary you want.
Using the ast.literal_eval().
It can be used for conversion of other data types as well
# importing the module
import ast
# reading the data from the file
with open('dictionary.txt') as f:
data = f.read()
# reconstructing the data as a dictionary
d = ast.literal_eval(data)
print(d)
There are two methods for this,
1. Method using json.load():
.load() use to get from directly form file
import json
with open('data.json') as f:
json.load(f)
2. Method using json.loads():
.loads() use to get from string. So we need to read the file first to get string.
import json
with open('data.json') as f:
json.loads(f.read())
I have downloaded a compressed json file and want to open it as a dictionary.
I used json.load but the data type still gives me a string.
I want to extract a keyword list from the json file. Is there a way I can do it even though my data is a string?
Here is my code:
import gzip
import json
with gzip.open("19.04_association_data.json.gz", "r") as f:
data = f.read()
with open('association.json', 'w') as json_file:
json.dump(data.decode('utf-8'), json_file)
with open("association.json", "r") as read_it:
association_data = json.load(read_it)
print(type(association_data))
#The actual output is 'str' but I expect it is 'dic'
In the first with block you already got the uncompressed string, no need to open it a second time.
import gzip
import json
with gzip.open("19.04_association_data.json.gz", "r") as f:
data = f.read()
j = json.loads (data.decode('utf-8'))
print (type(j))
Open the file using the gzip package from the standard library (docs), then read it directly into json.loads():
import gzip
import json
with gzip.open("19.04_association_data.json.gz", "rb") as f:
data = json.loads(f.read(), encoding="utf-8")
To read from a json.gz, you can use the following snippet:
import json
import gzip
with gzip.open("file_path_to_read", "rt") as f:
expected_dict = json.load(f)
The result is of type dict.
In case if you want to write to a json.gz, you can use the following snippet:
import json
import gzip
with gzip.open("file_path_to_write", "wt") as f:
json.dump(expected_dict, f)
I have a json file that is a synonime dicitonnary in French (I say French because I had an error message with ascii encoding... due to the accents 'é',etc). I want to read this file with python to get a synonime when I input a word.
Well, I can't even read my file...
That's my code:
data=[]
with open('sortieDES.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
print(data)
So I have a list quite ugly, but my question is: how can I use the file like a dictionary ? I want to input data['Académie']and have the list of the synonime... Here an example of the json file:
{"Académie française":{
"synonymes":["Institut","Quai Conti","les Quarante"]
}
You only need to call json.load on the File object (you gave it the name data_file):
data=[]
with open('sortieDES.json', encoding='utf-8') as data_file:
data = json.load(data_file)
print(data)
Instead of
json.load(line)
you have to use
json.loads(line)
Your s is missing in loads(...)
I have a file called usernames.py that may contain a list or does exist at all:
usernames.py
['user1', 'user2', 'user3']
In Python I now want to read this file if it exists and append to the list a new user or create a list with that user i.e. ['user3']
This is what I have tried:
with open(path + 'usernames.py', 'w+') as file:
file_string = host_file.read()
file_string.append(instance)
file.write(file_string)
This gives me an error unresolved 'append'. How can I achieve this? Python does not know it is a list and if the file does not exist even worst as I have nothing to convert to a list.
Try this:
import os
filename = 'data'
if os.path.isfile(filename):
with open(filename, 'r') as f:
l = eval(f.readline())
else:
l = []
l.append(instance)
with open(filename, 'w') as f:
f.write(str(l))
BUT this is quite unsafe if you don't know where the file is from as it could include any code to do anything!
It would be better not to use a python file for persistence -- what happens if someone slips you a usernames.py that has exploit code in it? Consider a csv file or a pickle, or just a text file with one user per line.
That said, if you don't open it as a python file, something like this should work:
from os.path import join
with open( join(path, 'usernames.py'), 'r+') as file:
file_string = file.read()
file_string = file_string.strip().strip('[').strip(']')
file_data = [ name.strip().strip('"').strip("'") for name in file_string.split(',' )]
file_data.append( instance )
file.fseek(0)
file.write(str(file_data))
If usernames contain commas or end in quotes, you have to be more careful.
I have the following code:
logFile=open('c:\\temp\\mylogfile'+'.txt', 'w')
pprint.pprint(dataobject)
how can i send the contents of dataobject to the log file on the pretty print format ?
with open("yourlogfile.log", "w") as log_file:
pprint.pprint(dataobject, log_file)
See the documentation.
Please use pprint.pformat, which returns a formated string that can be dumped directly to file.
>>> import pprint
>>> with open("file_out.txt", "w") as fout:
... fout.write(pprint.pformat(vars(pprint)))
...
Reference:
http://docs.python.org/2/library/pprint.html
For Python 2.7
logFile = open('c:\\temp\\mylogfile'+'.txt', 'w')
pp = pprint.PrettyPrinter(indent=4, stream=logFile)
pp.pprint(dataobject) #you can reuse this pp.print