正则表达式TypeError:“str”和“int”的实例之间不支持“>”

2024-04-25 18:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我掌握的资料

 'E10641',
 'Type 1 diabetes mellitus with ',
 'hypoglycemia with coma',
 'E1110',
 'Type 2 diabetes mellitus with ketoacidosis ',
 'without coma',
 'E1100',
 'Type 2 diabetes mellitus with ',
 'hyperosmolarity withoutnonketotic ',
 'hyperglycemic-hyperosmolar coma ',
 '(NKHHC)',
 'E1111',
 'Type 2 diabetes mellitus with ketoacidosis ',
 'with coma',
 'Diabetes short-term complications diagnosis codes: (ACDIASD)',
 'June 2018',
 '3 of 3',

我正在尝试编写一个正则表达式代码来提取以“E”开头、后跟数字的代码,例如E10641。你知道吗

This is my program:
import re
content = str(content)
for line in content:
    if len (line>0):
        x = re.search("E[0-9]+", content)
        print (x)

但它有以下错误:

TypeError: '>' not supported between instances of 'str' and 'int'

我通过编辑iflen (line)>0一节修复了这个问题,正如这个问题的答案中所建议的那样。这是代码的更新版本:

import re
ICD = []

#content = str(content)
for line in content:
    if len (line) >0 :
        x = re.search("E[0-9]+", content)
        ICD = ICD.append(x)
        print (x)

我需要提取所有的代码并把它们放到一个列表中。但现在我有以下错误:

'NoneType' object has no attribute 'append'

你能帮帮我吗?你知道吗


Tags: of代码importretypewithlinecontent
1条回答
网友
1楼 · 发布于 2024-04-25 18:26:56

不太清楚你想做什么,但问题出在这一行 if len (line>0):您正在检查字符串是否大于int,并询问答案的长度是多少。你知道吗

我猜您想键入if len(line)>0:,而不需要检查它是否大于0,您只需使用if line: 空字符串为False,而非空字符串为True

>>> a="sss"
>>> b=""
>>> bool(a)
True
>>> bool(b)
False

相关问题 更多 >