2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
如果類別、項(xiàng)目和描述由雙換行符分隔,您可以使用此示例來(lái)解析它(regex101):
import re
txt = '''1. Food : Cake : Baked sweet food made from flour, sugar and other ingredients.
2. Electronics : Computer : A machine to carry out a computer programming operation.
Computers mainly consists of a CPU, monitor, keyboard and a mouse.
3. Automobile : Car : Car is a four wheeled motor vehicle used for transportation.'''
for cat, item, desc in re.findall(r'^(?:\d+)\.([^:]+):([^:]+):(.*?)(?:\n\n|\Z)', txt, flags=re.M|re.S):
print(cat)
print(item)
print(desc)
print('-' * 80)
印刷:
Food
Cake
Baked sweet food made from flour, sugar and other ingredients.
--------------------------------------------------------------------------------
Electronics
Computer
A machine to carry out a computer programming operation.
Computers mainly consists of a CPU, monitor, keyboard and a mouse.
--------------------------------------------------------------------------------
Automobile
Car
Car is a four wheeled motor vehicle used for transportation.
--------------------------------------------------------------------------------

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
這可能是一種基本方法,但它適用于您提供的示例輸入:
[0-9]+\s*.\s*(\w*)\s*:\s*(\w*)\s*:\s*((?:.*[\n\r]?)+?)(?=$|\d\s*\.)
基本上,我們?cè)诿枋鲋胁捎帽M可能多的文本(包括換行符),直到到達(dá)文件末尾或另一個(gè)數(shù)字索引。
添加回答
舉報(bào)