2

我尝试使用以下数据读取文本文件并说明其数据类型:

这是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

我试图显示他们的数据类型,但这是我的错误:

AttributeError:“列表”对象没有属性“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")
4

3 回答 3

2

该案例的工作解决方案(实际上有多个修复)

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-18T08:12:02.763 回答
2

问题是您的列表有 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
于 2019-12-18T08:47:58.103 回答
1
data.append(row.split(","))

split 返回一个列表,因此当 data 是列表列表时,当您运行时x.isalpha()x 是列表,而不是字符串。

不确定您的初衷是什么,但您可能想要更改

data += row.split(",")
于 2019-12-18T08:12:21.427 回答