Search code examples
pythonisalpha

Python list does not have isalpha()


I tried to read a text file with this following data and state its data type:

This is the data in the txt file

        9yr14z1jdnh01ou8d38        , rcsfhuivhqgpgabxd, szumg5l7lyc30wimkah4, 1345, yfeporesumr, 1313.670598592, 1384.35266, gklxmzulyylpuhkabawhuhtcxjy, ugwulzqwhld, 8422, 8728.054385, 1675,  9xuxp5l7trq50l6psgw058aqacs9n , 2080.01345, ppznnnmherwktxugprysryj, 4295, 6992,  g0aa4yh2btxc6ta959p9o

The output should be like this:

youruasdifafasd - alphabetical strings
127371237 - integer
asdfka12348fas - alphanumeric
13123.123 - real numbers
asjdfklasdjfklaasf - alphabetical strings
123192u3kjwekhf - alphanumeric

I tried to display their datatype but then this is the error I:

AttributeError: 'list' object has no attribute 'isalpha'

This is my code so far:

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

Solution

  • The problem is that your list has 2 dimensions and you get a list type in your for loop. If you print your data after with statement, you can see the 2 dimensions (nested lists).

    print(data)
    # [['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]]
    

    You can fix this issue if you change the .append() method to .extend() method.

    I have created a working version from your original implementation. I have used the output.txt which you have mentioned in your question.

    I don't have to define the data variable as an empty list. You should read the complete file and remove spaces and split the string based on , delimiter. The following line does it: data = file.read().replace(" ", "").split(",").

    Whit this solution you have a 1 dimension list and if you iterate through on it you will get the single elements. If you print the data variable, you can see: ['9yr14z1jdnh01ou8d38', 'rcsfhuivhqgpgabxd', 'szumg5l7lyc30wimkah4', ...]. It means you can get the elements one by one in your for loop the the isalpha() and isalnum() will work as expected.

    Code:

    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))
    

    Output:

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