简体   繁体   English

Python string.split 不符合我的预期

[英]Python string.split doesn't do what I was expecting

I have a string with a bunch of weirdness in it that I'd like to break up into a list:我有一个字符串,里面有一堆奇怪的东西,我想分解成一个列表:

"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}

Ideally, I'd like:理想情况下,我希望:

"44":{"1":4.6,"0":1.53,"2":7.2}
"53":{"1":4.2,"0":1.4,"2":6.75}
"121":{"1":3.2,"0":1.6,"2":6}

But I'd settle for splitting at each }.但我会满足于在每个} 处进行拆分。

mystring.split('}') seems to split my string into a list with one element per character for some reason. mystring.split('}')似乎出于某种原因将我的字符串拆分为每个字符一个元素的列表。 Not sure what I'm doing wrong.不知道我做错了什么。 Help!帮助!

That almost looks like valid JSON.几乎看起来像有效的 JSON。

>>> s = '"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}'
>>> import json
>>> d = json.loads("{" + s + "}")
>>> d
{'53': {'2': 6.75, '0': 1.4, '1': 4.2}, '44': {'2': 7.2, '0': 1.53, '1': 4.6}, '
121': {'2': 6, '0': 1.6, '1': 3.2}}
>>> for key,value in d.items():
...    print("Key: {0} - Value: {1}".format(key,value))
...
Key: 53 - Value: {'2': 6.75, '0': 1.4, '1': 4.2}
Key: 44 - Value: {'2': 7.2, '0': 1.53, '1': 4.6}
Key: 121 - Value: {'2': 6, '0': 1.6, '1': 3.2}

You could try this:你可以试试这个:

s = '"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}'
h = eval("{"+s+"}")
for k in h: print k,h[k]

but since eval is not safe as it can execute arbitrary code, it is much better to use literal_eval.但由于 eval 不安全,因为它可以执行任意代码,因此使用literal_eval 会好得多。 literal_eval only works on valid data types: literal_eval 仅适用于有效的数据类型:

from ast import literal_eval
s = # ....
h = literal_eval("{"+s+"}")
for k in h: print k,h[k]

Output输出

121 {'1': 3.2, '0': 1.6, '2': 6}
44 {'1': 4.6, '0': 1.53, '2': 7.2}
53 {'1': 4.2, '0': 1.4, '2': 6.75}

You can do something like this:你可以这样做:

>>> string = '"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}'
>>> string.replace('},','}***').split('***')
['"44":{"1":4.6,"0":1.53,"2":7.2}', '"53":{"1":4.2,"0":1.4,"2":6.75}', '"121":      {"1":3.2,"0":1.6,"2":6}']

Correct answer to your questions depends on what kind of expressions are allowed.您的问题的正确答案取决于允许使用的表达式类型。 If an expression may contain nested braces, then you need recursive regular expression.如果表达式可能包含嵌套的大括号,那么您需要递归正则表达式。 The following solution doesn't support nested braces, but it's enough to parse your sample:以下解决方案不支持嵌套大括号,但足以解析您的示例:

    for token in re.findall(r'((?:[^{,]|{.*?})+)', mystring):
        print token

Your input is nearly JSON;您的输入几乎是 JSON; it's the contents of a dict, just missing the enclosing braces :它是 dict 的内容,只是缺少括号

import json
nearly_json = '"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}'

d = json.loads(f'{{ {nearly_json} }}') # Add the missing enclosing braces, and convert to Python (deserialize)...

d
{'44': {'1': 4.6, '0': 1.53, '2': 7.2}, '53': {'1': 4.2, '0': 1.4, '2': 6.75}, '121': {'1': 3.2, '0': 1.6, '2': 6}}

d['44']
{'1': 4.6, '0': 1.53, '2': 7.2}

As TimPietzcker said you can now iterate over the dict eg with .items() Or you can access its .keys()正如 TimPietzcker 所说,您现在可以使用.items()迭代字典,或者您可以访问它的.keys()

Just split on the comma?只用逗号分开?

print '"44":{"1":4.6,"0":1.53,"2":7.2},"53":{"1":4.2,"0":1.4,"2":6.75},"121":{"1":3.2,"0":1.6,"2":6}'.split(',')

gives:给出:

['"44":{"1":4.6', '"0":1.53', '"2":7.2}', '"53":{"1":4.2', '"0":1.4', '"2":6.75}', '"121":{"1":3.2', '"0":1.6', '"2":6}']

which is what you asked for.这就是你所要求的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM