我使用 Python 解析带有收据的电子邮件以收集数据库。 我的部分代码无法正常工作。 代码:
# Concatenate lines starting with '&' or starting with lowercase to the end of the previous line
combined_lines = []
for line in soup.stripped_strings:
if (line.startswith('&') or line[0].islower()) and combined_lines:
combined_lines[-1] += ' ' + line.lstrip()
else:
combined_lines.append(line)
几个样品:
“拉瓦扎咖啡
和香气”;
“
洗碗机粉”
我希望通过我的代码获得“Lavazza Cofee & Aroma”和“洗碗机用粉末”。
用“&”符号连接效果很好,而小写则不行。
为了测试你的代码,我编写了这段代码,看起来它的工作原理。确定不行吗?
doc = '''Lavazza Cofee
& Aroma;
Powder for
dishwasher'''
combined_lines = []
for line in str.split(doc, '\n'):
print(f"line:{line}")
if len(combined_lines) and (line.startswith('&') or line[0].islower()):
combined_lines[-1] += ' ' + line.lstrip()
else:
combined_lines.append(line)
print(combined_lines)
结果如下。我打印行进行调试:
line:Lavazza Cofee
line:& Aroma;
line:Powder for
line:dishwasher
['Lavazza Cofee & Aroma;', 'Powder for dishwasher']