我尝试使用以下数据读取文本文件,并说明其数据类型:
这是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”对象没有属性“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")发布于 2019-12-18 08:47:58
问题是,您的列表有两个维度,并且在for循环中得到一个列表类型。如果在with语句之后打印数据,则可以看到二维(嵌套列表)。
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发布于 2019-12-18 08:12:02
案例的工作解决方案(实际上是多个修复)
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"),因为我们从文件中读取了第一个(或多个类似的行),所以我们可以扩展数据列表,而不是生成列表列表(
data.append(row.split(",")))。像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发布于 2019-12-18 08:12:21
data.append(row.split(","))split返回一个列表,所以当数据是一个列表时,所以当您运行x.isalpha() x时是一个列表,而不是一个字符串。
不知道你的初衷是什么,但你可能想要改变
data += row.split(",")https://stackoverflow.com/questions/59387777
复制相似问题