我想从content_list
中提取六个字段,并将它们放入一个数据帧中。字段为:Seq. #
、Name
、Coding Instructions
、Target Value
、Selections
和Supporting Definitions
。但是,我必须获取元数据对象的regex没有为列表中的每个项提供Seq. #
,并且缺少其他一些项,所以当我对其进行子集设置时,它会给我一个索引超出范围的错误。我不确定我做错了什么。你能帮我吗?非常感谢。
import re
import pandas as pd
content_list = ['nSeq. #:n2031', 'Name:nSSN N/AnThe value on arrival at this facilitynTarget Value:nSelection TextnDefinitionnNonYesnSelections:n(none)nSupporting Definitions:nIndicate the number created and automatically inserted by the software that uniquely identifies this patient.nCoding Instructions:nOnce assigned to a patient at the participating facility, this number will never be changed or reassigned to a different patient. If the npatient returns to the same participating facility or for followup, they will receive this same unique patient identifier.nNote(s):', 'nSeq. #:n2040', 'Name:nNCDR Patient IDnThe value on arrival at this facilitynTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nAn optional patient identifier, such as Medical Record Number, that can be associated with the patient.nCoding Instructions:nThis element is referenced in The Joint Commission AMI Core Measures, AMI-1 through AMI-5. AMI-7, 7a, 8, 8a and AMI-9.nNote(s):', 'nSeq. #:n2045', "Name:nOther IDnN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nIndicate the patient's date of birth.nCoding Instructions:nThis element is referenced in The Joint Commission AMI Core Measures, AMI-1 through AMI-5. AMI-7, 7a, 8, 8a and AMI-9.nNote(s):", 'nSeq. #:n2050', "Name:nBirth DatenThe value on arrival at this facilitynTarget Value:n(none)nSelections:n(none)nSupporting Definitions:n© 2007, American College of Cardiology Foundationn3/31/2014nPage 2 of 137nEffective for Patient Discharges January 01, 2015nCoder's Data DictionarynNCDR® ACTION Registry®-GWTGŽ v2.4nA. DemographicsnIndicate the patient's sex at birth.nCoding Instructions:nThis element is referenced in The Joint Commission AMI Core Measures, AMI-1 through AMI-5. AMI-7, 7a, 8, 8a and AMI-9.nNote(s):", 'nSeq. #:n2060', 'Name:nSexnThe value on arrival at this facilitynTarget Value:nSelection TextnDefinitionnMalenFemalenSelections:n(none)nSupporting Definitions:nIndicate if the patient is White.nCoding Instructions:nIf the patient has multiple race origins, specify them using the other race selections in addition to this one.nThis element is referenced in The Joint Commission AMI Core Measures, AMI-1 through AMI-5. AMI-7, 7a, 8, 8a and AMI-9.nNote(s):', 'nSeq. #:n2070', 'Name:nRace - WhitenThe value on arrival at this facilitynTarget Value:nSelection TextnDefinitionnNonYesnSelections:nWhite (race)n:nHaving origins in any of the original peoples of Europe, the Middle East, or North Africa.nSource:nU.S. Office of Management and Budget. Classification of Federal Data on Race and EthnicitynSupporting Definitions:nIndicate if the patient is Black or African American.nCoding Instructions:nIf the patient has multiple race origins, specify them using the other race selections in addition to this one.nThis element is referenced in The Joint Commission AMI Core Measures, AMI-1 through AMI-5. AMI-7, 7a, 8, 8a and AMI-9.nNote(s):','nSeq. #:n1040', 'Name:nTransmission NumbernN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nVendor Identification (agreed upon by mutual selection between the vendor and the NCDR) to identify software vendor. Vendors nmust use consistent name identification across sites. Changes to Vendor Name Identification must be approved by the NCDR.nCoding Instructions:', 'nSeq. #:n1050', "Name:nVendor IdentifiernN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nVendor's software product name and version number identifying the software which created this record (assigned by vendor). nVendor controls the value in this field. Version passing certification/harvest testing will be noted at the NCDR.nCoding Instructions:", 'nSeq. #:n1060', "Name:nVendor Software VersionnN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:n© 2007, American College of Cardiology Foundationn3/31/2014nPage 136 of 137nEffective for Patient Discharges January 01, 2015nCoder's Data DictionarynNCDR® ACTION Registry®-GWTGŽ v2.4nZ. AdministrationnThe NCDR Registry Identifier describes the data registry to which these records apply. It is implemented in the software at the time nthe data is collected and the records are created. This is entered into the schema automatically by software.nCoding Instructions:", 'nSeq. #:n1070', 'Name:nRegistry IdentifiernN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nRegistry Version describes the version number of the Data Specifications/Dictionary, to which each record conforms. It identifies nwhich fields should have data, and what are the valid data for each field. It is the version implemented in the software at the time nthe data is collected and the records are created. This is entered into the schema automatically by software.nCoding Instructions:', 'nSeq. #:n1080', 'Name:nRegistry VersionnN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:nReserved for future use.nCoding Instructions:', 'nSeq. #:n1200', "Name:nAuxiliary 0nN/AnTarget Value:n(none)nSelections:n(none)nSupporting Definitions:n© 2007, American College of Cardiology Foundationn3/31/2014nPage 137 of 137nEffective for Patient Discharges January 01, 2015nCoder's Data DictionarynNCDR® ACTION Registry®-GWTGŽ v2.4nZ. Administration"]
sequence_list = []
metadata = []
for i in content_list:
metadata = list(filter(None, re.split("s*(?:Seq. #:|Name:|Coding Instructions:|Target Value:|Selections:|Supporting Definitions:)s*", i)))
sequence_list.append([metadata[0], metadata[1], metadata[2], metadata[3], metadata[4], metadata[5]])
df = pd.DataFrame(sequence_list, columns = ['Seq #:','Name','Coding Instructions','Target Value','Supporting Definitions','Selections'])
df['Seq #:'] = df['Seq #:'].astype(int)
df.head()
您可以用换行符连接content_list
中的项目,然后用双换行符拆分生成的字符串以获得段落,稍后可以使用类似的匹配正则表达式进行解析
pattern = r'(?s)^Seq. #:s*(.*?)nName:s*(.*?)nTarget Value:s*(.*?)nSelections:s*(.*?)nSupporting Definitions:s*(.*?)(?:nCoding Instructions:s*(.*))?$'
请参阅regex演示。看起来Coding Instructions
可能会丢失,所以它在正则表达式中是可选的。
Python演示:
sequence_list = []
pattern = r'^Seq. #:s*(.*?)nName:s*(.*?)nTarget Value:s*(.*?)nSelections:s*(.*?)nSupporting Definitions:s*(.*?)(?:nCoding Instructions:s*(.*))?$'
for i in re.split(r'n{2,}', 'n'.join(content_list)):
m = re.match(pattern, i.strip(), re.S)
if m:
sequence_list.append(m.groups())
df = pd.DataFrame(sequence_list, columns = ['Seq #:','Name','Coding Instructions','Target Value','Supporting Definitions','Selections'])
请注意,只有正则表达式匹配时,才会解析每个段落,如果匹配,则稍后使用匹配的.groups()
数据填充数据帧。