简体   繁体   English

如何从.txt文件中提取由不同空间大小分隔的数据?

[英]How to extract data from .txt file that is separated by varying space size?

I am trying to extract data (date and time) from a .txt file, raise it one second and then compare it with the next piece of data so I can see if date and time are continuous. 我试图从.txt文件中提取数据(日期和时间),将其提高一秒,然后将其与下一条数据进行比较,以便查看日期和时间是否连续。

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 the digits varies (one space if the data on its right is two digits, two spaces if the data on its right is one digit). 问题是数字之间的空格数是变化的(如果右边的数据是两位数,则为一个空格,如果右边的数据为一位,则为两个空格)。

How can I extract the dates, increment one second and then compare them with the next date in the text without having to check for every space variation? 如何提取日期,增加一秒,然后将它们与文本中的下一个日期进行比较,而不必检查每个空间变化?

You can use string.split() , this splits the string by varying spaces . 您可以使用string.split() ,这会通过改变空格来拆分字符串。

And example - 例子 -

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

The split method of the string class already splits at any contiguous whitespace. 字符串类的split方法已经在任何连续的空格中分割。 So just do your_string.split() 所以只需要做your_string.split()

Adding to the answer by @Anand S Kumar, you could then use 添加@Anand S Kumar的答案,然后你可以使用

" ".join(s.split())

(space between double-quotes can be replaced by character(s) of your choice) to combine the text into a continuous string. (双引号之间的空格可以用您选择的字符替换),以将文本组合成连续的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM