0

Consider the following string:

'128.000000 5.000000 2-level SCT3030AL C:\\Users\\Tex\\Desktop\\Test 2 do Project GUI - Opt - Copy\\Vectors\\Ripple = 0.3\\TwoLev9.6kHz_Ripple_0.3.txt 9600.000000'

I want to split it into the following form:

['128.000000', '5.000000',  '2-level', 'SCT3030AL', 'C:\\Users\\Tex\\Desktop\\Test 2 do Project GUI - Opt - Copy\\Vectors\\Ripple = 0.3\\TwoLev9.6kHz_Ripple_0.3.txt', '9600.000000']

Obs.: The text is not always the same as this, but I want to split everything by whitespaces except what is in between ':' and 'txt'.

Thanks in advance!

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397

1 Answers1

1

Simplest would be to use regex (instead of re) as in

import regex as re

text = '128.000000 5.000000 2-level SCT3030AL C:\\Users\\Tex\\Desktop\\Test 2 do Project GUI - Opt - Copy\\Vectors\\Ripple = 0.3\\TwoLev9.6kHz_Ripple_0.3.txt 9600.000000'

rx = re.compile(r':.+?\.txt(*SKIP)(*FAIL)|\s+')

parts = rx.split(text)
print(parts)

This yields

['128.000000', '5.000000', '2-level', 'SCT3030AL', 'C:\\Users\\Tex\\Desktop\\Test 2 do Project GUI - Opt - Copy\\Vectors\\Ripple = 0.3\\TwoLev9.6kHz_Ripple_0.3.txt', '9600.000000']

See a demo on regex101.com.

Jan
  • 38,539
  • 8
  • 41
  • 69