我正在尝试从纯文本中获取字典,文本是结构化的并将其保存到字典中,但它不起作用我的代码如下:
regex = r"^(?:[\w \/]*\:(?:(?!\n\n).)*)|\* \* \*"
test_str = ("SAMPLE TYPE / MEDICAL SPECIALTY:Dermatology \nSAMPLE NAME: Epidermal Autograft \nSAMPLE NAME: Epidermal Autograft \nSAMPLE NAME: Epidermal Autograft \nSAMPLE NAME: Epidermal Autograft \nSAMPLE NAME: Epidermal Autograft \n\nDESCRIPTION:A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings. Epidermal autograft on Integra to the back and application of allograft to areas of the lost Integra, not grafted on the back. \n(Medical Transcription Sample Report) \n\n* * *\n\nPREOPERATIVE DIAGNOSIS: A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings. \nfim here\nPOSTOPERATIVE DIAGNOSIS: A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings. \n\nPROCEDURES PERFORMED: \n1\\. Epidermal autograft on Integra to the back (3520 cm2). \n2\\. Application of allograft to areas of the lost Integra, not grafted on the\nback (970 cm2). \n\nANESTHESIA:General endotracheal. \n\nESTIMATED BLOOD LOSS: Approximately 50 cc. \n\nBLOOD PRODUCTS RECEIVED: One unit of packed red blood cells. \n\nCOMPLICATIONS:None. \n\nINDICATIONS:The patient is a 26-year-old male, who sustained a 60% total body surface area flame burn involving the head, face, neck, chest, abdomen, back, bilateral upper extremities, hands, and bilateral lower extremities. He has previously undergone total burn excision with placement of Integra and an initial round of epidermal autografting to the bilateral upper extremities and hands. His donor sites have healed particularly over his buttocks and he returns for a second round of epidermal autografting over the Integra on his back utilizing the buttock donor sites, the extent they will provide coverage. \n\nOPERATIVE FINDINGS: \n1\\. Variable take of Integra, particularly centrally and inferiorly on the\nback. A fair amount of lost Integra over the upper back and shoulders. \n2\\. No evidence of infection. \n3\\. Healthy viable wound beds prior to grafting. \n\nPROCEDURE IN DETAIL: The patient was brought to the operating room and positioned supine. General endotracheal anesthesia was uneventfully induced and an appropriate time out was performed. He was then repositioned prone and perioperative IV antibiotics were administered. He was prepped and draped in the usual sterile manner. All staples were removed from the Integra and the adherent areas of Silastic were removed. The entire wound bed was further prepped with scrub brushes and more Betadine followed by a sulfamylon solution. Hemostasis of the wound bed was ensured using epinephrine-soaked Telfa pads. Following dermal tumescence of the buttocks, epidermal autografts were harvested 8 one-thousandths of an inch using the air Zimmer dermatome. These grafts were passed to the back table where they were meshed 3:1. The donor sites were hemostased using epinephrine-soaked Telfa and lap pads. Once all the grafts were meshed, we brought them back up onto the field, positioned them over the wounds beginning inferiorly and moving cephalad where we had best areas of Integra engraftment. We were happy with the lie of the grafts and they were stapled into place. The grafts were then overlaid with Conformant 2, which was also stapled into place. Utilizing all of his buttocks skin, we did not have enough to cover his entire back, so we elected to apply allograft to the cephalad and a few areas on his flanks where we had had poor Integra engraftment. Allograft was thawed and meshed 1:1. It was then brought up onto the field, trimmed to fit and stapled into place over the wound. Once the entirety of the posterior wounds on his back were covered out with epidermal autograft or allograft sulfamylon soaked dressings were applied. Donor sites on his buttocks were dressed in Acticoat and secured with staples. He was then repositioned supine and extubated in the operating room having tolerated the procedure without any apparent complications. He was transported to PACU in stable condition. \n\nKEYWORDS:dermatology, flame burns, body surface area, epidermal autograft, autograft, integra, integra engraftment, wound, grafts, epidermal, allograft\n")
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)
key=''
content=''
PEPDic={}
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
wild=match
if(wild != "\* \* \*"):
# print("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group().replace('\n', ' ')))
key=wild.split(':')[0]
print(key)
content=wild.split(':')[1:]
print(content)
PEPDic[key]=content
print(PEPDic[key])
我还想从文本中删除* * *
。我无法将其保存到字典中,而是提供以下error
:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
AttributeError: '_sre.SRE_Match' object has no attribute 'split'
答案 0 :(得分:1)
这似乎是你正在照顾的:
import re
rx = re.compile(r'^(?P<key>[/A-Z ]+:)(?P<value>(?:(?:(?!^[/A-Z ]+:).)+\n?)+)', re.MULTILINE)
result = {m.group('key'): m.group('value').strip() for m in rx.finditer(test_str)}
print(result)
我更改了表达式,使其更精确,并使用了字典理解而不是上面的所有代码
<小时/> 这产生了
{'SAMPLE TYPE / MEDICAL SPECIALTY:': 'Dermatology', 'SAMPLE NAME:': 'Epidermal Autograft', 'DESCRIPTION:': 'A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings. Epidermal autograft on Integra to the back and application of allograft to areas of the lost Integra, not grafted on the back. \n(Medical Transcription Sample Report)', 'PREOPERATIVE DIAGNOSIS:': 'A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings. \nfim here', 'POSTOPERATIVE DIAGNOSIS:': 'A 60% total body surface area flame burns, status post multiple prior excisions and staged graftings.', 'PROCEDURES PERFORMED:': '1\\. Epidermal autograft on Integra to the back (3520 cm2). \n2\\. Application of allograft to areas of the lost Integra, not grafted on the\nback (970 cm2).', 'ANESTHESIA:': 'General endotracheal.', 'ESTIMATED BLOOD LOSS:': 'Approximately 50 cc.', 'BLOOD PRODUCTS RECEIVED:': 'One unit of packed red blood cells.', 'COMPLICATIONS:': 'None.', 'INDICATIONS:': 'The patient is a 26-year-old male, who sustained a 60% total body surface area flame burn involving the head, face, neck, chest, abdomen, back, bilateral upper extremities, hands, and bilateral lower extremities. He has previously undergone total burn excision with placement of Integra and an initial round of epidermal autografting to the bilateral upper extremities and hands. His donor sites have healed particularly over his buttocks and he returns for a second round of epidermal autografting over the Integra on his back utilizing the buttock donor sites, the extent they will provide coverage.', 'OPERATIVE FINDINGS:': '1\\. Variable take of Integra, particularly centrally and inferiorly on the\nback. A fair amount of lost Integra over the upper back and shoulders. \n2\\. No evidence of infection. \n3\\. Healthy viable wound beds prior to grafting.', 'PROCEDURE IN DETAIL:': 'The patient was brought to the operating room and positioned supine. General endotracheal anesthesia was uneventfully induced and an appropriate time out was performed. He was then repositioned prone and perioperative IV antibiotics were administered. He was prepped and draped in the usual sterile manner. All staples were removed from the Integra and the adherent areas of Silastic were removed. The entire wound bed was further prepped with scrub brushes and more Betadine followed by a sulfamylon solution. Hemostasis of the wound bed was ensured using epinephrine-soaked Telfa pads. Following dermal tumescence of the buttocks, epidermal autografts were harvested 8 one-thousandths of an inch using the air Zimmer dermatome. These grafts were passed to the back table where they were meshed 3:1. The donor sites were hemostased using epinephrine-soaked Telfa and lap pads. Once all the grafts were meshed, we brought them back up onto the field, positioned them over the wounds beginning inferiorly and moving cephalad where we had best areas of Integra engraftment. We were happy with the lie of the grafts and they were stapled into place. The grafts were then overlaid with Conformant 2, which was also stapled into place. Utilizing all of his buttocks skin, we did not have enough to cover his entire back, so we elected to apply allograft to the cephalad and a few areas on his flanks where we had had poor Integra engraftment. Allograft was thawed and meshed 1:1. It was then brought up onto the field, trimmed to fit and stapled into place over the wound. Once the entirety of the posterior wounds on his back were covered out with epidermal autograft or allograft sulfamylon soaked dressings were applied. Donor sites on his buttocks were dressed in Acticoat and secured with staples. He was then repositioned supine and extubated in the operating room having tolerated the procedure without any apparent complications. He was transported to PACU in stable condition.', 'KEYWORDS:': 'dermatology, flame burns, body surface area, epidermal autograft, autograft, integra, integra engraftment, wound, grafts, epidermal, allograft'}