python - 正则表达式 - 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'
请问你能帮帮我吗?
解决方案
不太确定您要做什么,但问题出在这一行中
if len (line>0):
,您正在检查字符串是否大于 int,并且您正在询问该答案的长度是多少。
我猜你想输入if len(line)>0:
并且你不需要检查它是否大于 0,你可以只使用if line:
一个空字符串是 False 而一个非空字符串是 True
>>> a="sss"
>>> b=""
>>> bool(a)
True
>>> bool(b)
False
推荐阅读
- html - 在父 fiv 中居中多个 div 并缩放它们
- autodesk-viewer - 有没有办法判断对象是否被剪裁?
- batch-file - 根据文件名列表创建 txt
- python - 使用 ICA 去除 EEG 信号中的眨眼
- powershell - 内存中的 Powershell 编码问题
- php - Laravel 5.5 集合部分分组
- c++ - 如何利用尾部填充?
- security - 在 HTTP 到 HTTPS 重定向期间验证请求标头
- google-chrome-extension - 是否可以在左右单击 chrome 扩展图标时执行不同的操作
- php - 如何更正 WordPress Genesis 网站页脚中显示的错误年份?