1

I think what I'm trying to achieve is fairly common but I can't find reference for it on the internet; either that or I'm misphrasing what I'm trying to do.

This is the string I would like to split:

array_1:target:radec, 0:00:00.00, -90:00:00.0

I would like to split it by the first two colons (':'), and by the first comma & space (', '), such that I get

['array_1', 'target', 'radec', '0:00:00.00, -90:00:00.0']

I've tried to run split() with arguments twice on the original string, and it fails on the second split() because I'm trying to split something that's already a list. All the other answers I can find seem to focus on splitting the string by all instances of a delimiter, but I want the last field in the list 0:00:00.00, -90:00:00.0 to remain like it is.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • You can use `re.split`, as suggested [here](https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python). – costaparas Dec 18 '20 at 12:53
  • @costaparas the issue I'm seeing here is that of the colons that I do & don't want to split by, none have spaces either before or after. Also the case with second comma-space ', ' ; maybe I can somehow use regex... – distantaxis Dec 18 '20 at 13:02
  • You can split twice, once by space and comma, later just split the first part by colons. – Tatranskymedved Dec 18 '20 at 13:34
  • You would need to split on ',' then merge back the last two columns and split again the other elements of the array. You cannot run split again on the list but you can on list items. – Eric Darchis Dec 18 '20 at 14:47

2 Answers2

1

First split it by the first ", " (using maxsplit=1), then the first element of the resulting list split by ":":

s = "array_1:target:radec, 0:00:00.00, -90:00:00.0"

temp = s.split(", ", maxsplit=1)
temp[0] = temp[0].split(":")
result = temp[0] + [temp[1]]

The result:

['array_1', 'target', 'radec', '0:00:00.00, -90:00:00.0']
MarianD
  • 13,096
  • 12
  • 42
  • 54
0

How about

l1 = s.split()
l2 = l1[0].split(':') + l1[1:]

This will first split by whitespace separator, then split the first element (only) by a colon separator, and then join the lists. Result:

['array_1', 'target', 'radec,', '0:00:00.00,', '-90:00:00.0']
Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
  • Technically, this would work in this *very specific* case, but it is in no way generic -- better to use one of the methods suggested in the duplicate post. – costaparas Dec 18 '20 at 12:56
  • @costapars the problem is that he wants to split by colon in certain places and by whitespace in other places. So you can't solve this by using multiple separators (e.g. first splitting by whitespace and then by colon). – Jussi Nurminen Dec 18 '20 at 12:57
  • This works fairly well, but I'd like to not have the comma in the third list entry, and for the last two entries to be part of a single entry, i.e. '0:00:00.00, -90:00:00.0' and not '0:00:00.00,', '-90:00:00.0'. My apologies if I'm sounding a bit dense, I'm a beginner with this sort of thing. – distantaxis Dec 18 '20 at 13:04