Python 列表没有 isalpha()
Python list does not have isalpha()
我尝试读取包含以下数据的文本文件并说明其数据类型:
这是txt文件中的数据
9yr14z1jdnh01ou8d38 , rcsfhuivhqgpgabxd, szumg5l7lyc30wimkah4, 1345, yfeporesumr, 1313.670598592, 1384.35266, gklxmzulyylpuhkabawhuhtcxjy, ugwulzqwhld, 8422, 8728.054385, 1675, 9xuxp5l7trq50l6psgw058aqacs9n , 2080.01345, ppznnnmherwktxugprysryj, 4295, 6992, g0aa4yh2btxc6ta959p9o
输出应该是这样的:
youruasdifafasd - alphabetical strings
127371237 - integer
asdfka12348fas - alphanumeric
13123.123 - real numbers
asjdfklasdjfklaasf - alphabetical strings
123192u3kjwekhf - alphanumeric
我试图显示他们的数据类型,但这是错误 I:
AttributeError: 'list' object has no attribute 'isalpha'
到目前为止,这是我的代码:
import numbers
import os
import string
import pandas as pd
data = []
with open('output.txt') as file:
for row in file:
# row = row.strip()
row = row.replace(" ", "")
data.append(row.split(","))
# print(data)
for x in data:
# print (x)
if x.isalpha():
print (x + " - alphabetical strings")
elif x.isalnum():
print (x + " - alphanumeric")
elif isinstance(x, numbers.Integral):
print (x + " - integer")
else:
print (x + " - float")
案例的有效解决方案(实际上是多个修复)
data = []
with open('output.txt') as file:
for row in file:
row = row.strip().replace(" ", "")
data.extend(row.split(","))
for x in data:
if x.isnumeric():
print(x + " - integer")
elif x.isalpha():
print(x + " - alphabetical strings")
elif x.isalnum():
print(x + " - alphanumeric")
else:
print(x + " - float")
Since we read the first (or multiple similar rows) from the file we can extend data list instead of generating list of lists (data.append(row.split(","))
). Do it like data.extend(row.split(","))
输出
9yr14z1jdnh01ou8d38 - alphanumeric
rcsfhuivhqgpgabxd - alphabetical strings
szumg5l7lyc30wimkah4 - alphanumeric
1345 - integer
yfeporesumr - alphabetical strings
1313.670598592 - float
1384.35266 - float
gklxmzulyylpuhkabawhuhtcxjy - alphabetical strings
ugwulzqwhld - alphabetical strings
8422 - integer
8728.054385 - float
1675 - integer
9xuxp5l7trq50l6psgw058aqacs9n - alphanumeric
2080.01345 - float
ppznnnmherwktxugprysryj - alphabetical strings
4295 - integer
6992 - integer
g0aa4yh2btxc6ta959p9o - alphanumeric
data.append(row.split(","))
拆分 returns 一个列表,所以当数据是列表的列表时,所以当你是 运行 x.isalpha()
x 是一个列表,而不是一个字符串。
不确定您的初衷是什么,但您可能想要更改
data += row.split(",")
问题是您的列表有 2 个维度,并且您在 for 循环中得到了一个列表类型。如果您在 with
语句之后打印数据,您可以看到 2 个维度(嵌套列表)。
print(data)
# [['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]]
如果将 .append()
方法更改为 .extend()
方法,则可以解决此问题。
我已经根据您的原始实现创建了一个工作版本。我已经使用了您在问题中提到的output.txt
。
我不必将 data
变量定义为空列表。您应该阅读完整的文件并删除空格并根据 ,
分隔符拆分字符串。下面一行是这样做的:data = file.read().replace(" ", "").split(",")
.
使用这个解决方案,您有一个一维列表,如果您遍历它,您将获得单个元素。如果打印 data
变量,您可以看到:['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]
。这意味着您可以在 for 循环中一个一个地获取元素,isalpha()
和 isalnum()
将按预期工作。
代码:
import numbers
with open("output.txt") as file:
data = file.read().replace(" ", "").split(",")
for x in data:
if x.isalpha():
print("{} - alphabetical strings".format(x))
elif x.isalnum():
print("{} - alphanumeric".format(x))
elif isinstance(x, numbers.Integral):
print("{} - integer".format(x))
else:
print("{} - float".format(x))
输出:
>>>python3 test_file.py
9yr14z1jdnh01ou8d38 - alphanumeric
rcsfhuivhqgpgabxd - alphabetical strings
szumg5l7lyc30wimkah4 - alphanumeric
1345 - alphanumeric
yfeporesumr - alphabetical strings
1313.670598592 - float
1384.35266 - float
gklxmzulyylpuhkabawhuhtcxjy - alphabetical strings
ugwulzqwhld - alphabetical strings
8422 - alphanumeric
8728.054385 - float
1675 - alphanumeric
9xuxp5l7trq50l6psgw058aqacs9n - alphanumeric
2080.01345 - float
ppznnnmherwktxugprysryj - alphabetical strings
4295 - alphanumeric
6992 - alphanumeric
g0aa4yh2btxc6ta959p9o - alphanumeric
我尝试读取包含以下数据的文本文件并说明其数据类型:
这是txt文件中的数据
9yr14z1jdnh01ou8d38 , rcsfhuivhqgpgabxd, szumg5l7lyc30wimkah4, 1345, yfeporesumr, 1313.670598592, 1384.35266, gklxmzulyylpuhkabawhuhtcxjy, ugwulzqwhld, 8422, 8728.054385, 1675, 9xuxp5l7trq50l6psgw058aqacs9n , 2080.01345, ppznnnmherwktxugprysryj, 4295, 6992, g0aa4yh2btxc6ta959p9o
输出应该是这样的:
youruasdifafasd - alphabetical strings
127371237 - integer
asdfka12348fas - alphanumeric
13123.123 - real numbers
asjdfklasdjfklaasf - alphabetical strings
123192u3kjwekhf - alphanumeric
我试图显示他们的数据类型,但这是错误 I:
AttributeError: 'list' object has no attribute 'isalpha'
到目前为止,这是我的代码:
import numbers
import os
import string
import pandas as pd
data = []
with open('output.txt') as file:
for row in file:
# row = row.strip()
row = row.replace(" ", "")
data.append(row.split(","))
# print(data)
for x in data:
# print (x)
if x.isalpha():
print (x + " - alphabetical strings")
elif x.isalnum():
print (x + " - alphanumeric")
elif isinstance(x, numbers.Integral):
print (x + " - integer")
else:
print (x + " - float")
案例的有效解决方案(实际上是多个修复)
data = []
with open('output.txt') as file:
for row in file:
row = row.strip().replace(" ", "")
data.extend(row.split(","))
for x in data:
if x.isnumeric():
print(x + " - integer")
elif x.isalpha():
print(x + " - alphabetical strings")
elif x.isalnum():
print(x + " - alphanumeric")
else:
print(x + " - float")
Since we read the first (or multiple similar rows) from the file we can extend data list instead of generating list of lists (
data.append(row.split(","))
). Do it likedata.extend(row.split(","))
输出
9yr14z1jdnh01ou8d38 - alphanumeric
rcsfhuivhqgpgabxd - alphabetical strings
szumg5l7lyc30wimkah4 - alphanumeric
1345 - integer
yfeporesumr - alphabetical strings
1313.670598592 - float
1384.35266 - float
gklxmzulyylpuhkabawhuhtcxjy - alphabetical strings
ugwulzqwhld - alphabetical strings
8422 - integer
8728.054385 - float
1675 - integer
9xuxp5l7trq50l6psgw058aqacs9n - alphanumeric
2080.01345 - float
ppznnnmherwktxugprysryj - alphabetical strings
4295 - integer
6992 - integer
g0aa4yh2btxc6ta959p9o - alphanumeric
data.append(row.split(","))
拆分 returns 一个列表,所以当数据是列表的列表时,所以当你是 运行 x.isalpha()
x 是一个列表,而不是一个字符串。
不确定您的初衷是什么,但您可能想要更改
data += row.split(",")
问题是您的列表有 2 个维度,并且您在 for 循环中得到了一个列表类型。如果您在 with
语句之后打印数据,您可以看到 2 个维度(嵌套列表)。
print(data)
# [['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]]
如果将 .append()
方法更改为 .extend()
方法,则可以解决此问题。
我已经根据您的原始实现创建了一个工作版本。我已经使用了您在问题中提到的output.txt
。
我不必将 data
变量定义为空列表。您应该阅读完整的文件并删除空格并根据 ,
分隔符拆分字符串。下面一行是这样做的:data = file.read().replace(" ", "").split(",")
.
使用这个解决方案,您有一个一维列表,如果您遍历它,您将获得单个元素。如果打印 data
变量,您可以看到:['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]
。这意味着您可以在 for 循环中一个一个地获取元素,isalpha()
和 isalnum()
将按预期工作。
代码:
import numbers
with open("output.txt") as file:
data = file.read().replace(" ", "").split(",")
for x in data:
if x.isalpha():
print("{} - alphabetical strings".format(x))
elif x.isalnum():
print("{} - alphanumeric".format(x))
elif isinstance(x, numbers.Integral):
print("{} - integer".format(x))
else:
print("{} - float".format(x))
输出:
>>>python3 test_file.py
9yr14z1jdnh01ou8d38 - alphanumeric
rcsfhuivhqgpgabxd - alphabetical strings
szumg5l7lyc30wimkah4 - alphanumeric
1345 - alphanumeric
yfeporesumr - alphabetical strings
1313.670598592 - float
1384.35266 - float
gklxmzulyylpuhkabawhuhtcxjy - alphabetical strings
ugwulzqwhld - alphabetical strings
8422 - alphanumeric
8728.054385 - float
1675 - alphanumeric
9xuxp5l7trq50l6psgw058aqacs9n - alphanumeric
2080.01345 - float
ppznnnmherwktxugprysryj - alphabetical strings
4295 - alphanumeric
6992 - alphanumeric
g0aa4yh2btxc6ta959p9o - alphanumeric