如何从字符串或json(string)解析特定的子字符串?

时间:2019-01-25 09:50:22

标签: python json python-3.x

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

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格式
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-00和{{1} }

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

必填输出:

Temp

由于这是api调用的响应,因此权限数量不断变化。 举例来说:如果未在时间点配置权限Fromlist=['17','20'] Permission=['Allow ALL','Check ALL'] Permission_Num=['Allow All-00','Check ALL-05'] ,或者可能添加了新的权限,例如Check ALL,则权限Change ALL可能不会出现在响应中。因此,代码应确保检查格式perm ALL-dd的所有可能的权限。

我尝试了以下代码:

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)

2 个答案:

答案 0 :(得分:0)

不清楚要提取什么。无论如何,您必须使用正则表达式。在python中,该库为re

例如,如果您要将所有子串从from group 17提取到Allow ALL-00Allow 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个字符即可:

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

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

您可以执行以下操作(the answer to this question为我提供了帮助)

#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])

答案 1 :(得分:0)

您的问题非常不清楚。我认为您应该与要求您这样做的人澄清您的需求。

如果我听你的话,并且想处理你提到的json ...

d = {'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\nSwitch#'}

data = d['show permission allowed to 16']
data = data.split('\n')
data = [item.strip() for item in data]
print(data)
#['show permission to 16', 'School permissions from group 17:student to group 16:teacher:', 'Allow ALL-00', 'RTYAHY: FALSE', 'RTYAHY: FALSE', '', 'Switch#']

您将看到,这样做会将您的数据处理到一个简单的列表中。如果您有一个更明确的问题,我可能会解释代码。