python - Python3 isnumeric() 方法未按预期工作
问题描述
我只是开始探索其他一些 Python 方法,例如 isnumeric()。我有一个分配任务,我要从文本中搜索和提取数字。我已经发布了下面的代码。
handle = open("words3.txt")
for line in handle:
line = line.rstrip()
split = line.split()
for word in split:
x = word.isnumeric()
print(x)
有了这个,我得到了回溯......但我认为我已经遵循了 str.isnumeric() 的规则。有人可以告诉我我做错了什么吗?
File "assignment11.py", line 10, in <module>
x = word.isnumeric()
AttributeError: 'str' object has no attribute 'isnumeric'
解决方案
isnumeric()
适用于 Unicode 字符串。要将字符串定义为 Unicode,您可以像这样更改字符串定义:
s = u'This is my string'
#OR
s=unicode('some string', 'utf-8')
isnum = s.isnumeric()
或者您可以这样做以更改word
为string
:
x = str(word).isnumeric()
推荐阅读
- python - 如何对数据框中特定持续时间下的时间戳进行编号
- api - Microsoft Teams Graph API - 创建没有组 ID 的团队
- javascript - nodejs - 替换大文件中任何地方的字符串
- python - 如何在继承的数据类中创建可选字段?
- javascript - 根据屏幕大小更改按钮颜色
- pandas - 根据 groupby 条件更新列的值 - Pandas
- javascript - jQuery clone() 追加到多个元素而不是一个
- python - 破折号代码错误:TypeError:意外的关键字参数`selected_columns`
- javascript - JavaScript 重定向到另一个 JavaScript 页面
- sql - TOP 如何影响 SQL 性能