Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python列表没有isalpha()_Python_Isalpha - Fatal编程技术网

Python列表没有isalpha()

Python列表没有isalpha(),python,isalpha,Python,Isalpha,我试图读取包含以下数据的文本文件,并说明其数据类型: 这是txt文件中的数据 9yr14z1jdnh01ou8d38 , rcsfhuivhqgpgabxd, szumg5l7lyc30wimkah4, 1345, yfeporesumr, 1313.670598592, 1384.35266, gklxmzulyylpuhkabawhuhtcxjy, ugwulzqwhld, 8422, 8728.054385, 1675, 9xuxp5l7trq50l6psg

我试图读取包含以下数据的文本文件,并说明其数据类型:

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

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

由于我们从文件中读取了第一行(或多个类似行),因此可以扩展数据列表,而不是生成列表列表(
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

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

由于我们从文件中读取了第一行(或多个类似行),因此可以扩展数据列表,而不是生成列表列表(
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
split返回一个列表,因此当数据是一个列表列表时,当运行
x时也是如此。isalpha()
x是一个列表,而不是字符串

不确定你的初衷是什么,但你可能想改变

data += row.split(",")
split返回一个列表,因此当数据是一个列表列表时,当运行
x时也是如此。isalpha()
x是一个列表,而不是字符串

不确定你的初衷是什么,但你可能想改变

data += row.split(",")

问题是列表有两个维度,并且在for循环中得到了一个列表类型。如果使用
语句在
之后打印数据,则可以看到两个维度(嵌套列表)

如果将
.append()
方法更改为
.extend()
方法,则可以解决此问题

我已经从您的原始实现创建了一个工作版本。我使用了您在问题中提到的
output.txt

我不必将
数据
变量定义为空列表。您应该读取完整的文件,删除空格,并根据
分隔符拆分字符串。下面一行执行此操作:
data=file.read().replace(“,”).split(“,”)

在这个解决方案中,您有一个一维列表,如果您对它进行迭代,您将得到单个元素。如果打印
数据
变量,可以看到:
['9yr14z1jdnh01ou8d38','rcsfhuivhqgpgabxd','szumg5l7lyc30wimkah4',…]
。这意味着您可以在循环中逐个获取元素,因为
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
输出:

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

问题是列表有两个维度,并且在for循环中得到了一个列表类型。如果使用
语句在
之后打印数据,则可以看到两个维度(嵌套列表)

如果将
.append()
方法更改为
.extend()
方法,则可以解决此问题

我已经从您的原始实现创建了一个工作版本。我使用了您在问题中提到的
output.txt

我不必将
数据
变量定义为空列表。您应该读取完整的文件,删除空格,并根据
分隔符拆分字符串。下面一行执行此操作:
data=file.read().replace(“,”).split(“,”)

在这个解决方案中,您有一个一维列表,如果您对它进行迭代,您将得到单个元素。如果打印
数据
变量,可以看到:
['9yr14z1jdnh01ou8d38','rcsfhuivhqgpgabxd','szumg5l7lyc30wimkah4',…]
。这意味着您可以在循环中逐个获取元素,因为
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
输出:

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

请尝试
data.extend(row.split(“,”)
尝试
data.extend(row.split(“,”)
谢谢您的解释。现在很好用,谢谢你的解释。它现在工作得很好。我的错,现在我知道为什么了。谢谢你,罗恩!我的错,现在我知道为什么了。谢谢你,罗恩!迄今为止最好的算法。谢谢米兰!迄今为止最好的算法。谢谢米兰!