如何在python中转储没有引号的json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25611882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dump json without quotes in python
提问by Tampa
Here is how I dump a file
这是我转储文件的方法
with open('es_hosts.json', 'w') as fp:
json.dump(','.join(host_list.keys()), fp)
The results is
结果是
"a,b,c"
I would like:
我想:
a,b,c
Thanks
谢谢
回答by Jeff Sheffield
Use python's built-in string replace function
使用python内置的字符串替换功能
with open('es_hosts.json', 'w') as fp:
json.dump(','.join(host_list.keys()).replace('\"',''), fp)
回答by Falko
Before doing a string replace, you might want to stripthe quotation marks:
在进行字符串替换之前,您可能需要strip引号:
print '"a,b,c"'.strip('"')
Output:
输出:
a,b,c
That's closer to what you want to achieve. Even just removing the first and the last character works: '"a,b,c"'[1:-1].
这更接近您想要实现的目标。即使只是删除第一个和最后一个字符也有效:'"a,b,c"'[1:-1].
But have you looked into this question?
但是你有没有研究过这个问题?
回答by enrico.bacis
Well, that's not valid json, so the jsonmodule won't help you to write that data. But you can do this:
好吧,这不是有效的 json,因此该json模块不会帮助您编写该数据。但是你可以这样做:
import json
with open('es_hosts.json', 'w') as fp:
data = ['a', 'b', 'c']
fp.write(json.dumps(','.join(data)).replace('"', ''))
That's because you asked for json, but since that's not json, this should suffice:
那是因为您要求json,但由于那不是 json,所以这应该就足够了:
with open('es_hosts.json', 'w') as fp:
data = ['a', 'b', 'c']
fp.write(','.join(data))
回答by szmoore
To remove the quotation marks in the keysonly, which may be important if you are parsing it later (presumably with some tolerant parser or maybe you just pipe it directly into nodefor bizarre reasons), you could try the following regex.
要仅删除键中的引号,这在您稍后解析时可能很重要(大概是使用一些宽容的解析器,或者您可能node出于奇怪的原因直接将其输入),您可以尝试以下正则表达式。
re.sub(r'(?<!: )"(\S*?)"', '\1', json_string)
One issue is that this regex expects fields to be seperated key: valueand it will fail for key:value. You could make it work for the latter with a minor change, but similarly it won't work for variable amounts of whitespace after :
一个问题是这个正则表达式期望字段被分离key: value,并且它将失败key:value. 您可以通过稍作更改使其适用于后者,但同样地,它不适用于可变数量的空格之后:
There may be other edge cases but it will work with outputs of json.dumps, however the results will not be parseable by json. Some more tolerant parsers like yamlmight be able to read the results.
可能还有其他边缘情况,但它适用于 的输出json.dumps,但是结果将无法被 json 解析。一些更宽容的解析器yaml可能能够读取结果。
import re
regex = r'(?<!: )"(\S*?)"'
o = {"noquotes" : 127, "put quotes here" : "and here", "but_not" : "there"}
s = json.dumps(o)
s2 = json.dumps(o, indent=3)
strip_s = re.sub(regex,'\1',s)
strip_s2 = re.sub(regex,'\1',s2)
print(strip_s)
print(strip_s2)
assert(json.loads(strip_s) == json.loads(s) == json.loads(strip_s2) == json.loads(s2) == object)
Will raise a ValueErrorbut prints what you want.
将提高 aValueError但打印你想要的。
回答by kushal khanal
Just use for loop to assign list to string.
只需使用 for 循环将列表分配给字符串。
import json
with open('json_file') as f:
data = json.loads(f.read())
for value_wo_bracket in data['key_name']:
print(value_wo_bracket)
Note there is difference between json.loadand json.loads
注意json.load和之间有区别json.loads

