简体   繁体   English

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

[英]How to get only first element in list contained in string?

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.我可以使用json5.loads然后使用列表中的[0]获取第一个元素,但是json5.loads需要很长时间才能获得更长的字符串。 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. (在此示例中,它将是{"ex1": 0, "ex2":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}'这会打印出 '{"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]

Does your string work with ast.literal_eval() ?您的字符串是否适用于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.如果不是,您可以逐个字符地遍历字符串,并在左括号的数量等于右括号的数量时产生任何 substring。

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()如果你想将生成的子字符串解析为对象,你可以像你已经做的那样使用json5 ,这在较小的字符串上可能更快,或者使用ast.literal_eval()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查列表元素中是否包含字符串 - Check if string is contained in list element 如何用字符串替换重复项并仅保留列表列表中的第一个元素? - How to replace duplicates with string and keep only first element in the list of lists? 如何获取列表中字符串的第一个元素以及列表中的其余元素? - How to get first element of a string in a list and the rest in the list? 如何仅将touple列表的第一个元素放入数组 - how to get only the first element of a touple list into an array 检查Pandas中的元素(列表)中是否包含字符串 - Check whether a string is contained in a element (list) in Pandas 如何从列表中包含的字符串中仅获取一个字符 - how to get just one character form a string contained in a list Python-传递带有元素的列表,但是我只获得列表中的第一个元素 - Python - Passing a list with elements, but I only get the first element in the list 如何获取python列表的第一个元素ID - How to get python list first element id 带有Selenium的Python:如何获取元素中的第一个字符串 - Python with selenium: how to get the first string in element Python3:按元组第一个元素中包含的数据戳对元组列表进行排序 - Python3: Sorting a list of tuples by datastamp contained in first element of tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM