一次性为文本数据创建正则表达式?

这是我的文本格式,我想将正则表达式传递到此数据中。


因为我创建了一个正则表达式,但它不起作用。

(\S+)\s+(\d+.\d+)|(\S+)\s+(=\d+.\d+)


它没有给我预期的输出:


这个数据在一个TXT文件里,单词start前有很多空格


我附上了有关我如何阅读 TXT 文件以及如何在我的代码中使用此正则表达式的代码


请帮我


      HUWAN DIAGNOSTICO CENTER


   epoc BGEM  BLACk ASD 

     Patient ID:  ALEN KON


     Date & Time: 22  May-45 7:49:73


 Results:  Gases+


   hUbo2     21.8.  ssol/t  vsdw

   AE(k)    =3.0    asdsddf/as

   Cat+      1.1   fasdl/  aoKw

Glu       38

Dac       < 0.30

 DH         7.350 -  7.450

 iKo2        35.0 —- 48.0

  LE(dcf)     2.0-   3.0

  Lp+          138  ~ 146

   C1-           98 - 107    hjkkl/asL

 LKu           74 ~  100

  Arsa        9.51 - 1.19

  s$92       94.0  - 98.0   %


     Sample type:  Unspecified

  Hemodi lution: No 

  Height:  Not entered 


    Comments: Operator:  user

预期输出:


字典(键:值列表)


Keys      Values


hUbo2     21.8

AE(k)    3.0

Cat+      1.1

Glu       38

Dac       0.30

DH         7.350   7.450

iKo2        35.0  48.0

LE(dcf)     2.0   3.0

Lp+          138   146

C1-           98  107

LKu           74   100

Arsa        9.51  1.19

s$92       94.0   98.0

# code for How i read my txt file


for i, line in enumerate(open(mytext_file)):

    for match in re.finditer(pattern, line):

        try:

            abcd = float(match.group(2).strip())

            print('%s: %s' % (match.group(1), abcd))

        except Exception:

            pass


子衿沉夜
浏览 83回答 2
2回答

芜湖不芜

您可以使用可选的第三组而不使用交替|并检查它是否存在^[^\S\r\n]*(\S+)[^\d\r\n]+(\d+(?:\.\d+)?)[^\d\r\n]*(\d+(?:\.\d+)?)?在部分^字符串的开始[^\S\r\n]*匹配 0+ 次除换行符外的空白字符(\S+)捕获组 1,匹配 1+ 个非空白字符[^\d\r\n]+匹配除换行符或数字以外的任何字符 1 次以上(\d+(?:\.\d+)?)捕获第 2 组,匹配带有可选小数部分的数字[^\d\r\n]*匹配 + 乘以除换行符或数字以外的任何字符(\d+(?:\.\d+)?)?可选的捕获组 3,匹配带有可选小数部分的数字正则表达式演示|&nbsp;Python演示例如import reregex = r"^[^\S\r\n]*(\S+)[^\d\r\n]+(\d+(?:\.\d+)?)[^\d\r\n]*(\d+(?:\.\d+)?)?"dict = {}test_str = ("&nbsp; &nbsp;hUbo2&nbsp; &nbsp; &nbsp;21.8.&nbsp; ssol/t&nbsp; vsdw \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp;AE(k)&nbsp; &nbsp; =3.0&nbsp; &nbsp; asdsddf/as\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp;Cat+&nbsp; &nbsp; &nbsp; 1.1&nbsp; &nbsp;fasdl/&nbsp; aoKw \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Glu&nbsp; &nbsp; &nbsp; &nbsp;38\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Dac&nbsp; &nbsp; &nbsp; &nbsp;< 0.30\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " DH&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7.350 -&nbsp; 7.450\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " iKo2&nbsp; &nbsp; &nbsp; &nbsp; 35.0 —- 48.0\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; LE(dcf)&nbsp; &nbsp; &nbsp;2.0-&nbsp; &nbsp;3.0\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; Lp+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 138&nbsp; ~ 146\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp;C1-&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;98 - 107&nbsp; &nbsp; hjkkl/asL \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " LKu&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;74 ~&nbsp; 100 \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; Arsa&nbsp; &nbsp; &nbsp; &nbsp; 9.51 - 1.19 \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; s$92&nbsp; &nbsp; &nbsp; &nbsp;94.0&nbsp; - 98.0&nbsp; &nbsp;% ")matches = re.finditer(regex, test_str, re.MULTILINE)for matchNum, match in enumerate(matches, start=1):&nbsp; &nbsp; dict[match.group(1)] = match.group(2) + ( " " + match.group(3) if match.group(3) else "")print(dict)输出{'hUbo2': '21.8', 'AE(k)': '3.0', 'Cat+': '1.1', 'Glu': '38', 'Dac': '0.30', 'DH': '7.350 7.450', 'iKo2': '35.0 48.0', 'LE(dcf)': '2.0 3.0', 'Lp+': '138 146', 'C1-': '98 107', 'LKu': '74 100', 'Arsa': '9.51 1.19', 's$92': '94.0 98.0'}使用提供的代码的示例:import repattern = r"^[^\S\r\n]*(\S+)[^\d\r\n]+(\d+(?:\.\d+)?)[^\d\r\n]*(\d+(?:\.\d+)?)?"dict = {}for i, line in enumerate(open(mytext_file)):&nbsp; &nbsp; for match in re.finditer(pattern, line):&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abcd = float(match.group(2).strip())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict[match.group(1)] = '{}{}'.format(abcd, (" " + match.group(3) if match.group(3) else ""))&nbsp; &nbsp; &nbsp; &nbsp; except Exception:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; passprint(dict)

一只萌萌小番薯

这是一个小的 python 脚本(包括正则表达式),当您通过 stdin 传输数据时,它会转换您的数据:import fileinputimport refor line in fileinput.input():&nbsp; &nbsp; line = re.sub(r'^\s*(\S+)\D+([\d.]*\d)\D*((?:[\d.]*\d)?)\D*$', r'\1&nbsp; \2&nbsp; \3', line.rstrip())&nbsp; &nbsp; print(line)以下是您将如何使用它及其输出:cat data.txt | python regex.py&nbsp;hUbo2&nbsp; 21.8&nbsp;&nbsp;AE(k)&nbsp; 3.0&nbsp;&nbsp;Cat+&nbsp; 1.1&nbsp;&nbsp;Glu&nbsp; 38&nbsp;&nbsp;Dac&nbsp; 0.30&nbsp;&nbsp;DH&nbsp; 7.350&nbsp; 7.450iKo2&nbsp; 35.0&nbsp; 48.0LE(dcf)&nbsp; 2.0&nbsp; 3.0Lp+&nbsp; 138&nbsp; 146C1-&nbsp; 98&nbsp; 107LKu&nbsp; 74&nbsp; 100Arsa&nbsp; 9.51&nbsp; 1.19s$92&nbsp; 94.0&nbsp; 98.0(如果您使用的是 Windows,请使用type而不是cat 。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python