Python - JSON Load from file not working - python

So I am writing a basic multipurpose script which uses json to import a dictionary from a file but for some reason it doesn't save properly. I've looked all over and can't find anything relating to my exact problem.
Here is my code:
import json
dicti = json.loads(open('database.db'))
print(str(dicti))
But then I get this error:
TypeError: JSON object must be str, not TextIOWrapper.
So does anyone have any ideas on what the problem is? Thanks in Advance.
Note: Currently the file only has inside it:
{}

You want json.load for loading a file. json.loads is for loading from a string.

Related

How do i convert 'google.cloud.documentai.v1beta2.types.document.Document' into JSON?

I am using Google Cloud Document AI's Form Parser API. After i do the request to the API , I get a response with type google.cloud.documentai.v1beta2.types.document.Document. I tried to write it to JSON using json.dumps() but it gives JSONDecodeError because JSON.dumps() dont know how to serialize object of type google.cloud.documentai.v1beta2.types.document.Document.
I am confused how to convert this to JSON
Any Help Appreciated!
I just found out that the google.cloud.documentai.v1beta2.types.document.Document object inherits from proto.Message, which itself inherits from proto.MessageMeta. You can use the proto.MessageMeta.to_json function to convert the Document object to a json string, like so:
import json
from google.cloud.documentai_v1beta3 import Document
json_string = Document.to_json(document)
dict_obj = json.loads(json_string)
with open("document.json", mode='w') as my_file:
json.dump(dict_obj, my_file)
The source code for proto.Message was a little hard to find, so here it is: https://github.com/googleapis/proto-plus-python/blob/cfd5b6caca3fa9add89d8c69ea620505dd90dd7c/proto/message.py#L330
I solved my problem.
basically you have to write a function that explores the Document object, and then assemble entire JSON yourself by code.

How to use the biblib parser with a bibtex file stored in a pyhon variable?

I have a bibtex file that I get from the frontend and I'm trying to parse this file with biblib (a python library to parse bibtex files). Because I get the file from the frontend its not stored in a file on my computer. The file gets passed through a variable from the frontend to python and is then stored in the python variable fileFromFrontend. So I can use for example:
bibtexFile = fileFromFrontend.read()
to read the file.
now I'm trying to do something like the following to print the parsed file in the python terminal:
from pybtex.database.input import bibtex
parser = bibtex.Parser()
bibtexFile= parser.parse_file(fileFromFrontend)
print (bibtexFile.entries)
but then I get this error:
-->bibtexFile = parser.parse_file(filesFromFrontend)
-->with open_file(filename, encoding=self.encoding) as f:
-->AttributeError: __enter__
This is probably because the parser tries to open the file but he doesn't have to open this file, he just needs to read this file. I don't know what function of the biblib library to use for parsing the file from a variable and haven't found anything so far to solve my problem.
Hopefully somebody can help
thanks
According to documentation ( https://docs.pybtex.org/api/parsing.html ) there is methods
parse_string and parse_bytes which could work.
so like this
from pybtex.database.input import bibtex
parser = bibtex.Parser()
bibtexFile= parser.parse_bytes(fileFromFrontend.read())
print (bibtexFile.entries)
I don't have pybtex installed, so I couldn't try it myself. But try those methods. Parse_bytes and parse_string needs bib-format as second parameter. In examples that is bibtex, so I tried it here.

Trying to use json load with txt file

import json
f=open("99_jiayi.txt",'r',encoding = 'utf-8-sig')
a=json.load(f)
f.close()
https://i.stack.imgur.com/vD3M5.png
https://i.stack.imgur.com/QP0oW.png
I tried to turn the text file into a list in order to analyze the data
I used the "json load" and it worked on another file which is written in the same mode
But when i want to use it on another file it comes out the error
i searched google for a lot of time but still cant get the ans
Hope someone can help me with this question
i have some problem to express my thought with eng so if anyone cant understand what i am typing plz let me know tks!!
The error message points to "1":NR, which does not look like valid JSON, so it seems like a valid error.
Edit: try putting all NR within quotes.

Trouble retrieving data from kivy's jsonstore

I'm having issues retrieving data from a '.json' file if the key contains non-ascii characters.
To explain better i want to illustrate this issue with an example.
Say if i want to save data into a json file as follows
store = JsonStore('example.json')
store.put('André Rose', type = 'sparkling wine', comment = 'favourite')
Then I want to retrieve it as follows
store.get('André Rose')
this returns an error that says:
KeyError: 'Andr\xc3\xa9'
I believe the problem is the non-ascii character " é ".
so my question is how can I save stuffs like this into a json file, and retrieve without getting this key error?
"There is a bug in kivy 1.8.0 under Python 3. When you are using Kivy 1.8.0 and Python 3, URlRequest fails to convert the incoming data to JSON. If you are using this combination you'll need to add:" (Philips, Creating Apps in Kivy)
import json
data = json.loads(data.decode())
I'm not sure if this will help your particular problem, but I thought I might throw it out there.

How can I parse a YAML file from the web using PyYAML?

I need to get a YAML file from the web and parse it using PyYAMl, but i can't seem to find a way to do it.
import urllib
import yaml
fileToBeParsed = urllib.urlopen("http://website.com/file.yml")
pythonObject = yaml.open(fileToBeParsed)
print pythonObject
The error produced when runing this is:
AttributeError: 'module' object has no attribute 'open'
If it helps, I am using python 2. Sorry if this is a silly question.
I believe you want yaml.load(fileToBeParsed) and I would suggest looking at urllib2.urlopen if not the requests module.

Categories