从Json字符串中删除“[”?

2024-09-28 17:15:26 发布

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

我有一个来自MongoDB的JSON返回字符串

[{'api_calls_per_day': 0.29411764705882354, '_id': ObjectId('51948e5bc25f4b1d1c0d303a'), 'api_calls_total': 5, 'api_calls_with_key': 3, 'api_calls_without_key': 2}]

我想从上面的字符串中去掉小括号“[”和“]”,结果如下:

^{pr2}$

你知道怎么把小括号从字符串中去掉吗?谢谢


Tags: key字符串apiidjsonmongodbwithtotal
3条回答

使用字符串方法:

>>> a = "[string with brackets]"
>>> a.strip('[]')
"string with brackets"

它是Python,string是一个列表。这个怎么样

a = "[string with brackets]"

a[1:-1] # string without brackets (or without first and last char)

你得到一根绳子了吗?如果你是,那就是:

myString = MongoDBreturned[1:-1]

{a从a到substring}。在

如果它不是一个字符串,实际上是一个列表,那么只需获取第一个(也是唯一的元素),即json对象。在

^{pr2}$

相关问题 更多 >