Use str.split () to select parts of a string separating by consecutive spaces

1

I am receiving some lines of log, to which I generate a header with the data of some of the fields, as the first ip in the line.

To do this use a split of empty spaces and so I could select the information I wanted to use in the header.

But I realized that when the day of the log is less than 10 instead of writing the number with a leading zero, another empty space is generated and the script fails, because instead of selecting the ip, select the time.

For example, given these two lines with different dates:

  

< 7 > Feb 28 20:06:09 113.78.60.60 1,2013 / 06/02 00: 04: 03,891701036531, sunny, 0,

     

< 7 > Feb 8 20:06:09 113.78.60.60 1,2013 / 06/02 00: 04: 03,891701036531, sunny, 0,

By using the following code:

import sys
import time

Log = sys.stdin.readline()
Ip = Log.split(' ')[3]

print Ip + ',' + Log

with the first string the Ip is captured without problems ("113.78.60.60") but with the second one what is obtained is the time ("20:06:09").

    
asked by xav 16.10.2017 в 15:46
source

1 answer

0

If you specify str.split to start the chain every time it finds a space, it does exactly that:

>>> log = "<7>Feb  8 20:06:09 113.78.60.60 1"
>>> log.split(" ")
['<7>Feb', '', '8', '20:06:09', '113.78.60.60', '1']

What you want is for the chain to split whenever you find a space or any number of consecutive spaces. Interestingly, this is done by the function str.split by default:

>>> log = "<7>Feb  8 20:06:09 113.78.60.60 1"
>>> log.split()
['<7>Feb', '8', '20:06:09', '113.78.60.60', '1']

Just do not pass the argument sep .

Official Documentation :

  

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the beginning or end if the string has leading or trailing whitespace.

Free translation:

  

If sep is not specified or is None , a different division algorithm is applied: consecutive blanks are considered as a single separator and the result will not contain empty strings at the beginning or at the end if the string has initial or final blank spaces.

    
answered by 16.10.2017 / 16:01
source