如何从Python输出JSON?

2024-04-19 20:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

使用dict.toSource()dictVar写入外部python文件:

var dictVar = {'one':'1','two':'2'};
fileOut = new File('./out.py');    
fileOut.open("w","TEXT","????");
fileOut.write(dictVar.toSource());
fileOut.close();

这将导致输出.py地址:

({one:"1", two:"2"})

期望时:

({'one':"1", 'two':"2"})

问题:有没有一种方法可以编写一个字典,将其键名作为字符串写入输出文件(例如'one''two'而不是onetwo)?你知道吗


Tags: 文件textpynewvaropenoutone