简体   繁体   中英

File written by json.dump() not readable by json.load()

I'm creating a setup script for my Python project. The script reads build configurations from a json file like so:

with open('setup.conf','r') as configfile:
    config = json.load(configfile)

which works perfectly. Later in the script, I constrain myself to a part of that object and write this part to another file:

config = config[arg]
[...]
with open('kivy/app.conf','w') as appconfig:
    json.dump(config,appconfig)

which at least does not generate any errors. Upon startup of my main app, I then want to read the file I just created:

path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/app.conf','r') as configfile:
    config = json.load(configfile)

This, however, fails with a

simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

indicating that the json written by json.dump() itself is invalid from the very first character onwards. The data itself is as unsuspicious as it can get, all plain ASCII characters, no weird line endings etc.:

{"deploy_server": false, "run_server": true, "server": "127.0.0.1", "run_app": true, "deploy_iOS": false, "user": "", "debug": true, "path": "", "deploy_android": false, "port": "8000"}

I don't have the slightest idea where this could be coming from. Any help is greatly appreciated!

UPDATE

I discovered that the very same code above works in a live interpreter session. I conclude from this that there must be something strange going on in the code surrounding this, but I'm at loss here as well: There probably is an obvious elephant in the room, but I can't see it. The surrounding code looks like this:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from foodcalendar import CalendarWidget
from kivy.resources import resource_add_path
import os
import requests
import json


[...]

class MyApp(App):
    def __init__(self):
        super(MyApp,self).__init__()
        path = os.path.dirname(os.path.abspath(__file__))
        print path
        with open(path + '/app.conf','r') as configfile:
            for r in configfile:
                print r
                config = json.loads(r)
        self.server = config["server"]

UPDATE 2

It turns out that the error I'm facing is somehow related to the requests module: If I comment out import requests , everything works as expected, but I'm clueless as to why this happens, since the docs of the json and requests modules remain silent about any incompatibilities...

UPDATE 3

This seems to be a machine dependent issue. I ran my code on another machine, and there it ran flawlessly. Python is version 2.7.12, OS is Ubuntu 16.04 x86_64, kernel version is 4.4.0.38-generic on both machines...

I copied and pasted the text you supplied as being the file contents, into a variable.

>>> import json
>>> a=""" {"deploy_server": false, "run_server": true, "server": "127.0.0.1", "run_app": true, "deploy_iOS": false, "user": "", "debug": true, "path": "", "deploy_android": false, "port": "8000"}"""
>>> json.loads(a)
{'deploy_android': False, 'run_app': True, 'port': '8000', 'debug': True, 'deploy_server': False, 'server': '127.0.0.1', 'user': '', 'run_server': True, 'deploy_iOS': False, 'path': ''}
>>> 

All works. You could try this also. If you get the same result as I did with this as literal text, then you can be sure the error is with the reading and writing the file. You could try a 'print' of what you are about to feed to json.loads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM