如何使用string.split()拆分具有多个分隔符的字符串?

2024-09-25 00:32:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的列表中有几个字符串,如下所示:

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']

我想把它们分成“at”和“|”。 我希望它与下面类似:

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

我尝试使用split函数,但它只是在“|”处拆分字符串,而不是在“at”处拆分字符串


Tags: 字符串列表sepatlistsplittomcharles
3条回答

此代码获取list的每个string元素,并用|替换at,然后按|拆分,然后在适当的位置分配结果字符串的子列表

旁注:不要使用list作为变量名,因为它是语言内置的关键字

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)

输出:

[['Sep 10, 2020', '17:36', 'Kate'], ['Sep 10, 2020', '17:13', 'Charles']]

应该行得通

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_)

你为什么不一次做一个分隔符呢

请注意,Kate cosist的名称“at”,这使得“at”分隔符有点不合理

列表=[2020年9月10日17:36凯特”,“2020年9月10日17:13查尔斯”,“2020年9月10日16:00汤姆”,“2020年9月10日15:27斯维特拉娜”,“2020年9月10日13:38查尔斯”,“2020年9月10日12:46伊丽娜”,“2020年9月10日11:32罗恩”,“2020年9月10日10:45斯维特拉娜”,“2020年9月10日09:33斯维特拉娜”,“2020年9月9日15:46凯特,2009年9月2日]2020年9月9日12:18伊琳娜,2020年9月9日11:13伊琳娜,2020年9月9日10:39查尔斯,2020年9月9日9:34阿尔卡迪乌斯·西隆,2020年9月8日17:39查尔斯,2020年9月8日15:33伊琳娜,2020年9月8日13:38伊琳娜,2020年9月8日11:45查尔斯2020年9月08日10:27伊琳娜、2020年9月08日09:26迈克尔、2020年9月08日08:30凯特、2020年9月07日17:36斯维特拉娜、2020年9月07日17:01查尔斯、2020年9月07日14:23斯维特拉娜、2020年9月07日13:35斯维特拉娜、2020年9月07日13:12迈克尔、2020年9月07日11:57查尔斯、2020年9月07日10:41斯维特拉娜2020年9月7日09:35,查尔斯,2020年9月4日16:45,斯维特拉娜,2020年9月4日14:46,斯维特拉娜,2020年9月4日11:41,伊琳娜]

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

print(splitted)

输出: 【2020年9月10日】、【17:36】、【K】、【e】、【2020年9月10日】、【17:13】、【查尔斯】、【2020年9月10日】、【16:00】、【汤姆】、【2020年9月10日】、【15:27】、【斯维特拉娜】、【2020年9月10日】、【13:38】、【查尔斯】、【2020年9月10日】、【12:46】、【伊丽娜】、【2020年9月10日】、【11:32】、【罗恩】、【2020年9月10日】、【斯维特拉娜】、【10na、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”、“2020年9月8日”、“13:38”、“Irina”、“2020年9月8日”、“11:45”、“查尔斯”、“2020年9月8日”、“10:27”、“Irina”、“2020年9月8日”、“09:26”、“Michael”、“2020年9月8日”、“08:30”、“K”、“e”、“2020年9月7日”、“17:36”、“Svetlana”、“2020年9月7日”、“17:01”、“查尔斯”、“2020年9月7日”、“14:23”、“Svetlana”、“2007年9月7日”,2020',13:35',斯维特拉娜',2020年9月7日',13:12',迈克尔',2020年9月7日',11:57',查尔斯',2020年9月7日',10:41',斯维特拉娜',2020年9月7日',09:35',查尔斯',2020年9月4日',16:45',斯维特拉娜',2020年9月4日',14:46',斯维特拉娜',2020年9月4日',11:41',伊丽娜']

相关问题 更多 >