Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
11957 Näkymät

I needed to update the __openerp__.py file automatically, so I wrote the following code

import json as json 
with open('__openerp__.py','r+') as fl:     var = eval(fl.read())     var['data'].append('some_xml_file.xml')     fl.seek(0,0)     fl.truncate()     output = json.dumps(var, indent=4)     fl.write(output)

The problem with this code is that the json.dumps method outputs the key 

{...,
'installable': True,
...}


to 

{...,
"installable": true,
...}


So when the __openerp__.py file is being read again it will gives the error: 

NameError: name 'false' is not defined

I've written the following line to mend this problem

    ...
    output = output.replace(': false',': False').replace(': true',': True')
    fl.write(output)

My question is, How to update the __openerp__.py file correctly, i.e. as a python dictionary,  without appealing to this replace line of code


Avatar
Hylkää
Paras vastaus

Json dumps will bind the data and sends the result.

If it again needs to read, then you need to use Json Loads [json.loads() ] to unbind data created by dumps.


Avatar
Hylkää
Tekijä

I know that I didn't use json.dumps the standard way, but I did this as I needed to update the __openerp__.py and save it formatted.

If I replaced the 7th line with

output = str(var)

it will save it as a dictionary in one line.

So if you can help me in this request your help will be appreciated

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
0
tammik. 21
12562
2
jouluk. 19
8946
2
toukok. 15
6721
0
maalisk. 15
5084
4
maalisk. 25
40489