首页 > 解决方案 > 正则表达式 - TypeError: '>' 在 'str' 和 'int' 的实例之间不支持

问题描述

这是我拥有的一些数据

 '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'

len (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'

请问你能帮帮我吗?

标签: pythonregex

解决方案


不太确定您要做什么,但问题出在这一行中 if len (line>0):,您正在检查字符串是否大于 int,并且您正在询问该答案的长度是多少。

我猜你想输入if len(line)>0:并且你不需要检查它是否大于 0,你可以只使用if line: 一个空字符串是 False 而一个非空字符串是 True

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

推荐阅读