如何仅获取字符串中包含的列表中的第一个元素?

发布于 2025-01-22 17:40:15 字数 324 浏览 1 评论 0原文

我有一个长字符串。该字符串包含一个列表,例如

'[{"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2025-01-29 17:40:15

您的字符串是否可以与ast.literal_eval()一起使用?如果是这样,您可以做到,

obj = ast.literal_eval(s)
# obj[0] gives the first dict

如果不这样做,当开放式支架的数量等于近距离托架的数量时,您可以循环循环并产生任何子字符串。

def get_top_level_dict_str(s):
  open_br = 0
  close_br = 0
  open_index = 0
  for i, c in enumerate(s):
    if c == '{':
        if open_br == 0: open_index = i 
        open_br += 1
    elif c == '}':
        close_br += 1
        if open_br > 0 and open_br == close_br:
            yield s[open_index:i+1]
            open_br = close_br = 0

如果要将所得的子字符串解析为对象,则可以像已经这样做的那样使用json5,在较小的字符串上可能会更快,或使用ast.literal_eval()

x = get_top_level_dict_str(s)
# next(x) gives the substring
# then use json5 or ast.literal_eval()

Does your string work with ast.literal_eval()? If it does, you could do

obj = ast.literal_eval(s)
# obj[0] gives the first dict

If 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.

def get_top_level_dict_str(s):
  open_br = 0
  close_br = 0
  open_index = 0
  for i, c in enumerate(s):
    if c == '{':
        if open_br == 0: open_index = i 
        open_br += 1
    elif c == '}':
        close_br += 1
        if open_br > 0 and open_br == close_br:
            yield s[open_index:i+1]
            open_br = close_br = 0

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 use ast.literal_eval()

x = get_top_level_dict_str(s)
# next(x) gives the substring
# then use json5 or ast.literal_eval()
天涯沦落人 2025-01-29 17:40:15

如果肯定会是这种格式,则可以搜索开始和结束括号。

mystr = '[{"ex1": 0, "ex2":1}, {"ex3": 2, "ex4":3}]'
first = mystr.index("{")
last = mystr.index("}")
extracted = mystr[first:last+1]
print(extracted)

这是打印的
'{“ ex1”:0,“ ex2”:1}'

对于更复杂的字符串:

mystr = '[{"ex1": {"ex1.33": -1, "ex1.66": -2}, "ex2":1}, {"ex3": 2, "ex4":3}]'
n_open = 0
n_close = 0
first = mystr.index("{")
for ii in range(len(mystr)):
    if mystr[ii] == "{":
        n_open += 1
    elif mystr[ii] == "}":
        n_close += 1
    if n_open > 0 and n_open == n_close:
        break
extracted = mystr[first:ii+1]

If it'll definitely be that format, you can just search for the beginning and ending brackets.

mystr = '[{"ex1": 0, "ex2":1}, {"ex3": 2, "ex4":3}]'
first = mystr.index("{")
last = mystr.index("}")
extracted = mystr[first:last+1]
print(extracted)

this prints
'{"ex1": 0, "ex2":1}'

For a more complicated string:

mystr = '[{"ex1": {"ex1.33": -1, "ex1.66": -2}, "ex2":1}, {"ex3": 2, "ex4":3}]'
n_open = 0
n_close = 0
first = mystr.index("{")
for ii in range(len(mystr)):
    if mystr[ii] == "{":
        n_open += 1
    elif mystr[ii] == "}":
        n_close += 1
    if n_open > 0 and n_open == n_close:
        break
extracted = mystr[first:ii+1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文