Capturing usernames after List: tag

I am trying to create a list called "userlist" with all the user names listed next to "List:", my idea is to parse the string using "List:" and then split based on "," and put them in list, however I can’t fix the line, any data on how this can be achieved?

output=""" alias: tech.sw.host name: tech.sw.host email: tech.sw.host email2: tech.sw.amss type: email list look_elsewhere: /usr/local/mailing-lists/tech.sw.host text: List tech SW team list_supervisor: <username> List: username1,username2,username3,username4, : username5 Members: User1,User2, : User3,User4, : User5 """ #print output userlist = [] for line in output : if "List" in line: print line 
+4
source share
5 answers

Using regex , str.translate and str.split :

 >>> import re >>> from string import whitespace >>> strs = re.search(r'List:(.*)(\s\S*\w+):', ph, re.DOTALL).group(1) >>> strs.translate(None, ':'+whitespace).split(',') ['username1', 'username2', 'username3', 'username4', 'username5'] 

Here you can also create a dict that allows you to access any attribute:

 def func(lis): return ''.join(lis).translate(None, ':'+whitespace) lis = [x.split() for x in re.split(r'(?<=\w):',ph.strip(), re.DOTALL)] dic = {} for x, y in zip(lis[:-1], lis[1:-1]): dic[x[-1]] = func(y[:-1]).split(',') dic[lis[-2][-1]] = func(lis[-1]).split(',') print dic['List'] print dic['Members'] print dic['alias'] 

Output:

 ['username1', 'username2', 'username3', 'username4', 'username5'] ['User1', 'User2', 'User3', 'User4', 'User5'] ['tech.sw.host'] 
0
source

If it were me, I analyzed the entire input to have easy access to each field:

 inFile = StringIO.StringIO(ph) d = collections.defaultdict(list) for line in inFile: line = line.partition(':') key = line[0].strip() or key d[key] += [part.strip() for part in line[2].split(',')] print d['List'] 
+3
source

Try the following:

 for line in output.split("\n"): if "List" in line: print line 

When Python is prompted to treat a string as a collection, it treats each character in that string as a member of this collection (as opposed to every string that you are trying to execute).

You can say this by printing each line:

 >>> for line in ph: ... print line ... a l i a s : t e ... 

By the way, there are much better ways to handle this. I would recommend taking a look at the RegEx built-in library in Python: http://docs.python.org/2/library/re.html

0
source

Try using strip() to remove spaces and line breaks before performing validation:

 if 'List:' == line.strip()[:5]: 

this should fix the string you need, then you can extract the usernames using split(',') :

 usernames = [i for i in line[5:].split(',')] 
0
source

Here are my two solutions, which are essentially the same, but the first is easier to understand.

 import re output = """ ... """ # First solution: join continuation lines, the look for List # Join lines such as username5 with previous line # List: username1,username2,username3,username4, # : username5 # becomes # List: username1,username2,username3,username4,username5 lines = re.sub(r',\s*:\s*', ',', output) for line in lines.splitlines(): label, values = [token.strip() for token in line.split(':')] if label == 'List': userlist = userlist = [user.strip() for user in values.split(',')] print 'Users:', ', '.join(userlist) # Second solution, same logic as above # Different means tokens, = [line for line in re.sub(r',\s*:\s*', ',', output).splitlines() if 'List:' in line] label, values = [token.strip() for token in tokens.split(':')] userlist = userlist = [user.strip() for user in values.split(',')] print 'Users:', ', '.join(userlist) 
0
source

Source: https://habr.com/ru/post/1495598/


All Articles