如何从字符串或json

如何从字符串或json

本文介绍了如何从字符串或json(string)解析特定的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,是来自api调用的响应:

I have one string which is response from api call:

x='{"show permission allowed to 16": "show permission to 16\\nSchool permissions from group 17:student to group 16:teacher:\\n\\tAllow ALL-00\\nSchool permissions from group 18:library to group 16(Temp):teacher:\\n\\tNo Allow ALL-00\\nSchool permissions from group 20:Gym to group 16:teacher:\\n\\tCheck ALL-00\\nRTYAHY: FALSE\\nRTYAHY: FALSE\\n\\n#"}'

这是x

json={'show permission allowed to 16': 'show permission to 16\nSchool permissions from group 17:student to group 16:teacher:\n\tAllow ALL-00\nSchool permissions from group 18:library to group 16(Temp):teacher:\n\tNo Allow ALL-09\nSchool permissions from group 20:Gym to group 16:teacher:\n\tCheck ALL-05\nRTYAHY: FALSE\nRTYAHY: FALSE\n\nSwitch#'}

我必须提取子字符串from group之后的数字(在上面的示例中,要提取的数字是1720)以及权限Allow ALLCheck ALL.权限的格式为Allow ALL-dd,其中dd的范围可以为00FF,我还应提取格式为Allow ALL-00Check ALL-05

I have to extract the the numbers after the substring from group (in the example above the numbers to be extracted are 17 and 20) and the permissions Allow ALL and Check ALL. PermissionS are in the format Allow ALL-dd where dd can be from 00 to FF and I should also extract the permissions in the format Allow ALL-00 and Check ALL-05

由于第二个权限No Allow ALL-09具有Temp,因此必须跳过此子字符串,并且不应存储该权限.

Since a second permission No Allow ALL-09 has Temp, this substring must be skipped and the permission shouldn't be stored.

必填输出:

Fromlist=['17','20']

Permission=['Allow ALL','Check ALL']

Permission_Num=['Allow All-00','Check ALL-05']

由于这是api调用的响应,因此权限数量一直在变化.例如,如果未在时间上配置权限Check ALL,或者可能添加了新的权限,例如Change ALL,则权限Check ALL可能不会出现在响应中.因此,代码应确保检查所有可能的格式为perm ALL-dd的权限.

Since this is the response of api call, number of permissions keeps changing.Say for example: Permission Check ALL might not appear in response if it is not configured at that point of time or a new permission like Change ALL might be added. So the code should be such that it should check for all possible permissions of format perm ALL-dd.

我已经尝试过以下代码:

I have tried this code:

l=x.find("permissions from group ")+len("permissions from group ")
print(l)
y=x[l:l+2] #this is to extract 17
from.append(y)
if((x.find("permissions from group ")) and (x.find('\\t'))):
    l=x.find('\\t')+len('\\t')
    e=x.find('-00') #Here I want to have -dd where dd can be 00 to ff
    s=x[l:e]
    perm.append(s)

推荐答案

不清楚要提取的内容.无论如何,您必须使用正则表达式.在python中,库为re.

It is not very clear what you want to extract. Anyway you must use regular expressions. In python the library is re.

例如,如果您要将所有子字符串从from group 17提取到Allow ALL-00Allow ALL-dd:

So for example if you want to extract the all substring from from group 17 to Allow ALL-00 or Allow ALL-dd:

import re
str1 = '{"show permission allowed to 16": "show permission to 16\\nSchool permissions from group 17:student to group 16:teacher:\\n\\tAllow ALL-00\\nRTYAHY: FALSE\\nRTYAHY: FALSE\\n\\n#"}'
res = re.sub(r'.*(from group 17.*Allow ALL-..).*', r'\1', str1)

如果相反,您只需要Allow ALL-之后的2个字符即可:

If instead you need just the 2 characters after Allow ALL-:

t = re.sub(r'.*from group 17.*Allow ALL-(..).*', r'\1', str1)

编辑2(根据您问题的最后修改)

您可以执行以下操作(此问题的答案帮助我取得了结果)

You can do the following (the answer to this question helped me with the result):

#creates a list of tuples. Each tuple has as first element
#the number found after 'from group ' (reppresented by the 2
#dots in the regular expression and the second element is a string
# containing all the characters after `\t` until 2 characters after the
# string  'ALL-'
res = re.findall(r'from group (\d+)(?:(?!Temp).)*?[^(Temp)]?(?:(?!Temp).)*?\\t(.*? ALL-..)',str1)
#[('17', 'Allow ALL-00'), ('20', 'Check ALL-00')]
#build the lists you want as output
#we build the lists you want as output
Fromlist= list()#['17','20']
Permission=list()#['Allow ALL','Check ALL']
Permission_Num=list()#['Allow All-00','Check ALL-05']
for el in res:
    Fromlist.append(el[0])
    Permission.append(el[1].split('-')[0])
    Permission_Num.append(el[1])

这篇关于如何从字符串或json(string)解析特定的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:51