简体   繁体   中英

Python string split without common delimiter

I am fairly new to Python. An external simulation software I use gives me reports which include data in the following format:

1    29 Jan 2013 07:33:19.273    29 Jan 2013 09:58:10.460          8691.186

I am looking to split the above data into four strings namely;

'1', '29 Jan 2013 07:33:19.273', '29 Jan 2013 09:58:10.460', '8691.186'

I cannot use str.split since it splits out the date into multiple strings. There appears to be four white spaces between 1 and the first date and between the first and second dates. I don't know if this is four white spaces or tabs.

Using '\\t' as a delimiter on split doesn't do much. If I specify ' ' (4 spaces) as a delimiter, I get the first three strings. I also then get an empty string and leading spaces in the final string. There are 10 spaces between the second date and the number.

Any suggestions on how to deal with this would be much helpful!

Thanks!

You can split on more than one space with a simple regular expression:

import re

multispace = re.compile(r'\s{2,}')  # 2 or more whitespace characters
fields = multispace.split(inputline)

Demonstration:

>>> import re
>>> multispace = re.compile(r'\s{2,}')  # 2 or more whitespace characters
>>> multispace.split('1    29 Jan 2013 07:33:19.273    29 Jan 2013 09:58:10.460          8691.186')
['1', '29 Jan 2013 07:33:19.273', '29 Jan 2013 09:58:10.460', '8691.186']

If the data is fixed width you can use character addressing in the string

n=str[0]
d1=str[2:26]
d2=str[27:51]
l=str[52:]

However, if Jan 02 is shown as Jan 2 this may not work as the width of the string may be variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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