Firdhaus Saleh
Firdhaus Saleh

Reputation: 189

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

Upvotes: 2

Views: 4227

Answers (3)

milanbalazs
milanbalazs

Reputation: 5329

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

Upvotes: 2

Ron Serruya
Ron Serruya

Reputation: 4446

data.append(row.split(","))

split returns a list, so when data is a list of lists so when you are running x.isalpha() x is a list, not a string.

Not sure what your original intent is, but you might want to change

data += row.split(",")

Upvotes: 1

Alexandr Shurigin
Alexandr Shurigin

Reputation: 3981

Working solution for the case (multiple fixes actually)

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

Output

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

Upvotes: 2

Related Questions