【发布时间】:2019-07-23 18:03:33
【问题描述】:
我的问题有点奇怪,也许有人可以提供一些指导。我有一行文本需要搜索并提取多个重复出现的字符串来填充数据框。给定以下行:
txt = "Name : 'red' Wire : 'R' Name : 'blue' Wire: 'B' Name : 'orange' Name: 'yellow' Wire : 'Y'"
我想通过正则表达式并提取仅完整的名称/电线对(在此示例中不是 Orange)。
预期输出
Name Wire
red R
blue B
yellow Y
代码
for line in txt:
line = line.strip()
a = re.search(r' Name : \'((?:(?![(]).)*)\'', line)
if a:
b = re.search(r' Wire : \'((?:(?![(]).)*)\'', line)
if b:
df = df.append({'Name' : a.group(1), 'Wire' : b.group(1)}, ignore_index=True)
此代码生成以下 df:
Name Wire
red R
这种行为是意料之中的,因为re.search() 只会运行直到它第一次找到有问题的项目。
好的,re.search() 不起作用,所以我会尝试 re.findall() 代替:
for line in txt:
line = line.strip()
a = re.findall(r' Name : \"((?:(?![(]).)*)\"', line)
if a:
b = re.findall(r' Wire : \"((?:(?![(]).)*)\"', line)
if b:
df = df.append({'Name' : a, 'Wire' : b}, ignore_index=True)
这将吐出以下df:
Name Wire
['red','blue','orange','yellow'] ['R','B','Y']
这个数据框的问题是,现在我们不再知道 Name 与 Wire 相关联。如果 re.search() 没有到达 txt 行的末尾,是否有任何方法可以让 re.search() 在第一次命中后继续?任何人都对如何仅针对包含所有内容的元素(即“名称”AND“连线”)正则表达式文本行有任何创意吗?
【问题讨论】: