How to extract data from a .txt file that is split by resizing space?

I am trying to extract data (date and time) from a file .txt

, lift it up by one second, and then compare it with the next piece of data so I can see if the date and time is continuous.

For example:

15  6 19  9 12 59.0000000  
... some other stuff...  
15  6 19  9 13  0.0000000

      

The problem is that the number of spaces between digits changes (one space if there are two digits to the right of it, two spaces if the data to the right of it has one digit).

How can I extract dates, increment one second and then compare them to the next date in the text without checking for each space change?

+3


source to share


3 answers


You can use string.split()

this to split the string by changing spaces.

And an example -



>>> s = "Hello  Bye          Test"
>>> s
'Hello  Bye          Test'
>>> s.split()
['Hello', 'Bye', 'Test']

      

+3


source


The split

string class method is already split for any contiguous spaces. So justyour_string.split()



+1


source


Adding to @Anand S Kumar's answer, you could use

" ".join(s.split())

(the space between the double quotes can be replaced with the character (s) of your choice) to concatenate the text into a continuous string.

0


source







All Articles