-1

I have a list element which is text.

print ((temp_list))

Output:

['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']

I wish to get this output:

Aug 7 14:23

I have tried to remove the whitespace but that messes up the output, which makes it harder to separate out the elements I want.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40

5 Answers5

2

You can split the text and get the 5th, 6th and 9th fields:

f = temp_list[0].split()
print(' '.join((f[4], f[5], f[8])))
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

Using Regex.

import re
temp_list = ['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']

for i in temp_list:
    m = re.search(r"(?P<date>(Jun|Jul|Aug|Sep).*?)\(", i)
    if m:
        print(m.group('date'))

Output:

Aug  7 14:22 - 14:23 
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1
sample = 'root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    '

# split the string on space characters
data = sample.split(' ')

# inspect our list in console, the list should now contain mix of words and spaces (empty string)
print(data)

# since empty string evaluates to False in Python, we can remove them like this from our list with filter function
data = filter(lambda x: x, data)


# outputs: ['root', 'pts/3', '100.121.17.73', 'Tue', 'Aug', '7', '14:22', '-', '14:23', '(00:00)']
print(data)

# in the end we collect relevant data by slicing the list
# from index 3rd to 6th and join them into one string with that data separated by one space in-between.
result = ' '.join(data[3:6])

# outputs: Tue Aug 7
print(result)
BJT
  • 648
  • 5
  • 16
  • Please edit your answer to add an explanation of how your code works and how it solves the OP's problem. Many StackOverflow users are newbies and will not understand the code you have posted, so will not learn from your answer. – i alarmed alien Aug 09 '18 at 13:58
  • @ialarmedalien noted – BJT Aug 09 '18 at 14:04
  • It will also be flagged as low quality (which is how I saw it) and may be removed. – i alarmed alien Aug 09 '18 at 14:15
  • @ialarmedalien alright, why low quality?, the rest did not leave much more info, i posted simplest answer, one that can be understood with basic python. I dont want to leave here bad content it is not my intention, if you think it is bad in any way please do delete it. One can always ask for more details if needed, no? – BJT Aug 09 '18 at 14:34
  • Unfortunately many people who come here don't even have basic python, and what seems obvious to a seasoned programmer will be like black magic to a newbie. Often people will just copy and paste an answer without understanding what's going on in the code, and they're the ones who will be back again with very similar questions because they didn't learn anything from the previous answer they used. The commented answer is excellent -- upvoted. :) – i alarmed alien Aug 09 '18 at 14:41
  • @ialarmedalien noted, thank you on clarification. You are right, i hurry to much. – BJT Aug 09 '18 at 14:53
0

If you always have the kind of pattern 'Tue Aug 7 14:22 - 14:23' in your string, then I suggest you using regex to match this pattern:

import re

temp_list = ['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']

m = re.search(r'\w{3} +(\w{3}) +(\d{1,2}) +\d\d:\d\d +- +(\d\d:\d\d)', temp_list[0])

result = ' '.join([m.group(i) for i in (1,2,3)])

print(result)  # Aug 7 14:23
Laurent H.
  • 6,316
  • 1
  • 18
  • 40
0

Or:

l=['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']
print(' '.join(l[0].split()[-6:][:-1]))

Output:

Aug  7 14:22 - 14:23 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114