0

I have this code:

 def saveInfo(self):
    
            info = {}
            info["title"] : self.getHeading()
            info["user"] : self.getUser()
            info["location"] : self.file_path
            info["uploaded"] : False
    
            print(info)
            self.writeToJSONFile(self.workspace, 'ri', info)

but whenever i print the 'info' or try to save in json, I only get this printed: {} and json file be like: {}{}{}{}...

1

1 Answer 1

3

You're mixing dictionary assignment and literal syntax (see the docs) and accidentally not causing syntax errors because it looks like a type annotation.

The idiomatic way to construct this dictionary would be:

info = {
    "title": self.getHeading(),
    "user": self.getUser(),
    "location": self.file_path,
    "uploaded": False,
}

Alternatively, you can assign each key one at a time with the = operator (not :!):

info = {}
info["title"] = self.getHeading()
info["user"] = self.getUser()
info["location"] = self.file_path
info["uploaded"] = False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.