Read json file as input and output as pprint? [duplicate] - python

This question already has answers here:
How to prettyprint a JSON file?
(15 answers)
Closed 5 years ago.
I'm working with a large json file that is currently encoded as one long line.
This makes it unintelligable for other people to work with, so I want to render it using pprint.
At the moment I'm trying to import the full file and print as pprint but my output looks like this:
<_io.TextIOWrapper name='hash_mention.json' mode='r' encoding='UTF-8'>
My question is- what is that showing? How can I get it to output the json data as pprint?
The code I've written looks like this:
import pprint
with open('./hash_mention.json', 'r') as input_data_file:
pprint.pprint(input_data_file)

You opened the file in read mode but forgot to read the file contents.
Just change pprint.pprint(input_data_file) with pprint.pprint(input_data_file.read()) and voila!

Related

How to convert python code in a text file to python code inside a program [duplicate]

This question already has answers here:
How can I parse (read) and use JSON?
(5 answers)
Closed 2 years ago.
So I have a text file with the text below:
{'Maps': {'Usefulness':80,'Accesibility':70,'Popularity':90}
(the dictionary carries on that's why there may be a few syntax issues)
How do I convert this text into python code inside my program?
eg:
if the text files name is x.txt
textfile = convert(x.txt)
print(list(textfile.keys()))
the output would look something like
['Maps','Drive','Chrome']
You can use ast.literal_eval to evaluate a string (like the one you get from reading a text file) as Python code:
import ast
s = """{
'Maps': {
'Usefulness':80,
'Accesibility':70,
'Popularity':90
}
}"""
ast.literal_eval(s)
# output: {'Maps': {'Usefulness': 80, 'Accesibility': 70, 'Popularity': 90}}
Reading from a text file:
with open('file.txt', 'r') as file:
result = ast.literal_eval(file.read())
print(list(result.keys()))
If your text file is just a dictionary you can also use the json module. But the more generalist answer is to use ast.literal_eval.
You should use the json module
import json
with open('x.txt') as json_file:
data = json.load(json_file)
print(data.keys)

Can't load a dictionary in python 3.6 using json.load [duplicate]

This question already has answers here:
Reading JSON from a file [duplicate]
(7 answers)
Closed 3 years ago.
I've been stuck on this for like an hour and I just can't find my mistake even though it has to be somewhere since I know json.load loads the json file into the dictionary and json.dump copies the dictionary to the .json file
p.s the dump does work but the load returns an empty dict
I tried to read the file conventionally and then use the .loads function, did the same thing
prefix = json.load(open('file.json', 'r'))
print("Prefixes currently are:"+str(prefix)) # prints {} even though the file includes : {"551475283459309599": "!", "557678616054464512": "!", "558760765348249609": "!", "559361893861556240": "%"}
I expected it to just do what it should (load the dict or a string or something but it loads nothing)
I tried running it on a different python project and it worked for some odd reason so the problem is probably somewhere else, thanks regardless!!
Try following the way to load json file:
import json
with open('file.json') as json_data:
d = json.load(json_data)
print("Prefixes currently are:"+str(d))

Adding 'r' to json string from file python [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 3 years ago.
I am facing with the following error while reading json from file
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 1 (char 948)
The json content is read from file using the script below
import json
if __name__ == "__main__":
file_path = "D:\\Freelancing\\Scraping_football_historic_data\\Data\\1.138103502"
with open(file_path,'r') as datafile:
dict_data = json.load(datafile)
print(dict_data)
Upon searching for answer, this question had an answer that suggested me to add r before the json string.
How can it be done in the case above, or if there's a better way to read the file. ?
The contents of the file can be read from the pastebin link:
https://pastebin.com/ZyyrtcZW
You are missing a comma between each of your individual dictionaries, your data should look like
....
{"op":"mcm","clk":"5733948534","pt":1514206953689,"mc":[{"id":"1.138103502","rc":[{"ltp":2.02,"id":48756}]}]},
{"op":"mcm","clk":"5739085003","pt":1514309273736,"mc":[{"id":"1.138103502","rc":[{"ltp":2.0,"id":48756}]}]},
{"op":"mcm","clk":"5739711407","pt":1514327265235,"mc":[{"id":"1.138103502","rc":[{"ltp":2.06,"id":48756}]}]},
.....

python readline() output nothing [duplicate]

This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 6 years ago.
I got nothing back from the command readline(). I am new to python and totally confused now.
my_file = open("test.txt", "w+")
my_file.write("This is a test")
print my_file.readline()
When you write to a file, you overwrite any previous contents of the file and leave the pointer at the end of the file. Any attempt to read after that will fail, since you're already at the end of the file.
To reset to the beginning of the file and read what you just wrote, use:
my_file.seek(0)
Because after you wrote content in you file. the cursor is at the end of the file. Before you use readline(), use my_file.seek(0) first, If your file content is only This is a test, you can get your want. Deep into this, please go to : https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files

read a pdf in python [duplicate]

This question already has answers here:
How to read line by line in pdf file using PyPdf?
(3 answers)
Closed 7 years ago.
I want to read a pdf file in python. Tried some of the ways- PdfReader and pdfquery but not getting the result in string format. Want to have some of the content from that pdf file. is there any way to do that?
PDFminer is a tool for extracting information from PDF documents.
Does it matter in your case if file is pdf or not. If you just want to read your file as string, just open it as you would open a normal file.
E.g.-
with open('my_file.pdf') as file:
content = file.read()

Categories

Resources