如何仅获取字符串中包含的列表中的第一个元素?
我有一个长字符串。该字符串包含一个列表,例如
'[{"ex1": 0, "ex2":1}, {"ex3": 2, "ex4":3}]'
我可以使用json5.5.loads
,然后通过在列表上使用[0]
获得第一个元素,但是json5。 LOADS
需要长时间的时间来完成更长的字符串。有没有办法仅在不加载整个列表的情况下获得第一个元素? (在此示例中,它将是{“ ex1”:0,“ ex2”:1}
。逗号分裂对我不起作用,因为列表中的字典中包含逗号。谢谢。
I've got a long string. This string contains a list, like such example
'[{"ex1": 0, "ex2":1}, {"ex3": 2, "ex4":3}]'
I can use json5.loads
and then get the first element by using [0]
on the list, but json5.loads
takes a long time for longer strings. Is there a way to get just the first element without loading the entire list? (in this example it would be {"ex1": 0, "ex2":1}
. Splitting by commas doesn't work for me since there are commas contained in dictionaries in the list. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的字符串是否可以与
ast.literal_eval()
一起使用?如果是这样,您可以做到,如果不这样做,当开放式支架的数量等于近距离托架的数量时,您可以循环循环并产生任何子字符串。
如果要将所得的子字符串解析为对象,则可以像已经这样做的那样使用
json5
,在较小的字符串上可能会更快,或使用ast.literal_eval()
Does your string work with
ast.literal_eval()
? If it does, you could doIf not, you could loop through the string character-by-character and yield any substring when the number of open-brackets are equal to the number of close-brackets.
If you want to parse the resulting substrings to objects, you could use
json5
like you already do, which is probably faster on the smaller string, or useast.literal_eval()
如果肯定会是这种格式,则可以搜索开始和结束括号。
这是打印的
'{“ ex1”:0,“ ex2”:1}'
对于更复杂的字符串:
If it'll definitely be that format, you can just search for the beginning and ending brackets.
this prints
'{"ex1": 0, "ex2":1}'
For a more complicated string: