0

I have a few strings in a list similar to the below:

list=['Sep 10, 2020 at 17:36 | Kate', 'Sep 10, 2020 at 17:13 | Charles', 'Sep 10, 2020 at 16:00 | Tom', 'Sep 10, 2020 at 15:27 | Svetlana', 'Sep 10, 2020 at 13:38 | Charles', 'Sep 10, 2020 at 12:46 | Irina', 'Sep 10, 2020 at 11:32 | Ron ', 'Sep 10, 2020 at 10:45 | Svetlana', 'Sep 10, 2020 at 09:33 | Svetlana', 'Sep 09, 2020 at 15:46 | Kate', 'Sep 09, 2020 at 14:02 | Svetlana', 'Sep 09, 2020 at 12:41 | Michael', 'Sep 09, 2020 at 12:18 | Irina', 'Sep 09, 2020 at 11:13 | Svetlana', 'Sep 09, 2020 at 10:39 | Charles', 'Sep 09, 2020 at 09:34 | Arkadiusz Sieron', 'Sep 08, 2020 at 17:39 | Charles', 'Sep 08, 2020 at 15:33 | Svetlana', 'Sep 08, 2020 at 13:38 | Irina', 'Sep 08, 2020 at 11:45 | Charles', 'Sep 08, 2020 at 10:27 | Irina', 'Sep 08, 2020 at 09:26 | Michael', 'Sep 08, 2020 at 08:30 | Kate', 'Sep 07, 2020 at 17:36 | Svetlana', 'Sep 07, 2020 at 17:01 | Charles', 'Sep 07, 2020 at 14:23 | Svetlana', 'Sep 07, 2020 at 13:35 | Svetlana', 'Sep 07, 2020 at 13:12 | Michael', 'Sep 07, 2020 at 11:57 | Charles', 'Sep 07, 2020 at 10:41 | Svetlana', 'Sep 07, 2020 at 09:35 | Charles', 'Sep 04, 2020 at 16:45 | Svetlana', 'Sep 04, 2020 at 14:46 | Svetlana', 'Sep 04, 2020 at 11:41 | Irina']

I want to split them at "at" and "|". I want it similar to below:

'Sep 10, 2020', '17:36', 'Kate'

I tried using the split function but Its just splitting the string at "|" and not at "at".

raptorzee
  • 151
  • 1
  • 11

3 Answers3

0

Why dont you do it one delimiter at a time?

Pay attention that the name Kate cosist "at", fact that make the "at" delimiter a bit non reasonable

list = ['Sep 10, 2020 at 17:36 | Kate', 'Sep 10, 2020 at 17:13 | Charles', 'Sep 10, 2020 at 16:00 | Tom', 'Sep 10, 2020 at 15:27 | Svetlana', 'Sep 10, 2020 at 13:38 | Charles', 'Sep 10, 2020 at 12:46 | Irina', 'Sep 10, 2020 at 11:32 | Ron ', 'Sep 10, 2020 at 10:45 | Svetlana', 'Sep 10, 2020 at 09:33 | Svetlana', 'Sep 09, 2020 at 15:46 | Kate', 'Sep 09, 2020 at 14:02 | Svetlana', 'Sep 09, 2020 at 12:41 | Michael', 'Sep 09, 2020 at 12:18 | Irina', 'Sep 09, 2020 at 11:13 | Svetlana', 'Sep 09, 2020 at 10:39 | Charles', 'Sep 09, 2020 at 09:34 | Arkadiusz Sieron', 'Sep 08, 2020 at 17:39 | Charles', 'Sep 08, 2020 at 15:33 | Svetlana', 'Sep 08, 2020 at 13:38 | Irina', 'Sep 08, 2020 at 11:45 | Charles', 'Sep 08, 2020 at 10:27 | Irina', 'Sep 08, 2020 at 09:26 | Michael', 'Sep 08, 2020 at 08:30 | Kate', 'Sep 07, 2020 at 17:36 | Svetlana', 'Sep 07, 2020 at 17:01 | Charles', 'Sep 07, 2020 at 14:23 | Svetlana', 'Sep 07, 2020 at 13:35 | Svetlana', 'Sep 07, 2020 at 13:12 | Michael', 'Sep 07, 2020 at 11:57 | Charles', 'Sep 07, 2020 at 10:41 | Svetlana', 'Sep 07, 2020 at 09:35 | Charles', 'Sep 04, 2020 at 16:45 | Svetlana', 'Sep 04, 2020 at 14:46 | Svetlana', 'Sep 04, 2020 at 11:41 | Irina']

splitted = []
for item in list:
    a = item.split("at")
    for part in a:
        splitted = splitted + part.split("|")

print(splitted)

output: ['Sep 10, 2020 ', ' 17:36 ', ' K', 'e', 'Sep 10, 2020 ', ' 17:13 ', ' Charles', 'Sep 10, 2020 ', ' 16:00 ', ' Tom', 'Sep 10, 2020 ', ' 15:27 ', ' Svetlana', 'Sep 10, 2020 ', ' 13:38 ', ' Charles', 'Sep 10, 2020 ', ' 12:46 ', ' Irina', 'Sep 10, 2020 ', ' 11:32 ', ' Ron ', 'Sep 10, 2020 ', ' 10:45 ', ' Svetlana', 'Sep 10, 2020 ', ' 09:33 ', ' Svetlana', 'Sep 09, 2020 ', ' 15:46 ', ' K', 'e', 'Sep 09, 2020 ', ' 14:02 ', ' Svetlana', 'Sep 09, 2020 ', ' 12:41 ', ' Michael', 'Sep 09, 2020 ', ' 12:18 ', ' Irina', 'Sep 09, 2020 ', ' 11:13 ', ' Svetlana', 'Sep 09, 2020 ', ' 10:39 ', ' Charles', 'Sep 09, 2020 ', ' 09:34 ', ' Arkadiusz Sieron', 'Sep 08, 2020 ', ' 17:39 ', ' Charles', 'Sep 08, 2020 ', ' 15:33 ', ' Svetlana', 'Sep 08, 2020 ', ' 13:38 ', ' Irina', 'Sep 08, 2020 ', ' 11:45 ', ' Charles', 'Sep 08, 2020 ', ' 10:27 ', ' Irina', 'Sep 08, 2020 ', ' 09:26 ', ' Michael', 'Sep 08, 2020 ', ' 08:30 ', ' K', 'e', 'Sep 07, 2020 ', ' 17:36 ', ' Svetlana', 'Sep 07, 2020 ', ' 17:01 ', ' Charles', 'Sep 07, 2020 ', ' 14:23 ', ' Svetlana', 'Sep 07, 2020 ', ' 13:35 ', ' Svetlana', 'Sep 07, 2020 ', ' 13:12 ', ' Michael', 'Sep 07, 2020 ', ' 11:57 ', ' Charles', 'Sep 07, 2020 ', ' 10:41 ', ' Svetlana', 'Sep 07, 2020 ', ' 09:35 ', ' Charles', 'Sep 04, 2020 ', ' 16:45 ', ' Svetlana', 'Sep 04, 2020 ', ' 14:46 ', ' Svetlana', 'Sep 04, 2020 ', ' 11:41 ', ' Irina']

Yossi Levi
  • 1,258
  • 1
  • 4
  • 7
0

This code takes each string element of the list and replaces at with | and then it splits by | and then assigns in-place the sub-list of the resulting strings.

Side-note: Don't use list as a variable name, since it is a language built-in keyword.

lis = ['Sep 10, 2020 at 17:36 | Kate', 'Sep 10, 2020 at 17:13 | Charles']
lis = [string.replace(" at ", " | ").split(" | ") for string in lis]
print(lis)

Output:

[['Sep 10, 2020', '17:36', 'Kate'], ['Sep 10, 2020', '17:13', 'Charles']]
solid.py
  • 2,782
  • 5
  • 23
  • 30
0

It should Work

list=['Sep 10, 2020 at 17:36 | Kate', 'Sep 10, 2020 at 17:13 | Charles', 'Sep 10, 2020 at 16:00 | Tom', 'Sep 10, 2020 at 15:27 | Svetlana', 'Sep 10, 2020 at 13:38 | Charles', 'Sep 10, 2020 at 12:46 | Irina', 'Sep 10, 2020 at 11:32 | Ron ', 'Sep 10, 2020 at 10:45 | Svetlana', 'Sep 10, 2020 at 09:33 | Svetlana', 'Sep 09, 2020 at 15:46 | Kate', 'Sep 09, 2020 at 14:02 | Svetlana', 'Sep 09, 2020 at 12:41 | Michael', 'Sep 09, 2020 at 12:18 | Irina', 'Sep 09, 2020 at 11:13 | Svetlana', 'Sep 09, 2020 at 10:39 | Charles', 'Sep 09, 2020 at 09:34 | Arkadiusz Sieron', 'Sep 08, 2020 at 17:39 | Charles', 'Sep 08, 2020 at 15:33 | Svetlana', 'Sep 08, 2020 at 13:38 | Irina', 'Sep 08, 2020 at 11:45 | Charles', 'Sep 08, 2020 at 10:27 | Irina', 'Sep 08, 2020 at 09:26 | Michael', 'Sep 08, 2020 at 08:30 | Kate', 'Sep 07, 2020 at 17:36 | Svetlana', 'Sep 07, 2020 at 17:01 | Charles', 'Sep 07, 2020 at 14:23 | Svetlana', 'Sep 07, 2020 at 13:35 | Svetlana', 'Sep 07, 2020 at 13:12 | Michael', 'Sep 07, 2020 at 11:57 | Charles', 'Sep 07, 2020 at 10:41 | Svetlana', 'Sep 07, 2020 at 09:35 | Charles', 'Sep 04, 2020 at 16:45 | Svetlana', 'Sep 04, 2020 at 14:46 | Svetlana', 'Sep 04, 2020 at 11:41 | Irina']
rep_data = []
for i in list:
     sp = i.split("at")
     new_ = sp[0] + str(sp[1].replace(" | ", ""))
     rep_data.append(new_)

Md Jewele Islam
  • 939
  • 8
  • 18