[英]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.