在datafram中的子字符串后提取字符串

2024-06-16 23:30:57 发布

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

'(ep1270399)\nname=stet, johannes cornelis p/a ballast nedam infra b.v., p.o. box 1526 , city=3430 bm  nieuwegein , country=nl \n\nname=bos, wilhelmus johannes p/a ballast nedam infra b.v., p.o. box 1526 , city=3430 bm  nieuwegein , country=nl \n'

我有一个pandas数据帧,我想提取总是在某个关键字\nname=之后的名称。因此,我想得到'stet'和'bos'并将其放入一个数组中。你知道吗


Tags: boxcitynlcountrybminfrajohannesbos
1条回答
网友
1楼 · 发布于 2024-06-16 23:30:57

假设您提供的字符串是一个字符串(基于引号)

import re

string = '(ep1270399)\nname=stet, johannes cornelis p/a ballast nedam infra b.v., p.o. box 1526 , city=3430 bm nieuwegein , country=nl \n\nname=bos, wilhelmus johannes p/a ballast nedam infra b.v., p.o. box 1526 , city=3430 bm nieuwegein , country=nl \n'

split = re.split(' |=|,|\n', string)
result = [split[idx + 1] for idx, value in enumerate(split) if value == 'name']

result

['stet', 'bos']

这允许您提取\nname=之后的所有值。但是,如果这些数据存储方式不同,您需要在问题中显示,以便我能更好地为您量身定制答案!你知道吗

不过,您应该能够将regex转换为任何格式。你知道吗

相关问题 更多 >