简体   繁体   中英

The following code is giving me an error: 'list' object has no attribute 'split'

I am trying to decipher a message from a text file by getting the mode of each column and is giving me an attribute error:

'list' object has no attribute 'split'

I know that list has no split , only strings have split .

file1=open("input.txt","r")
for row_ele in file1.readlines().split("\n"):
    c=0
    for ele_in_row in row_ele:
           #c=row_ele.count(ele_in_row):
            if c<row_ele.count(ele_in_row):
                c=row_ele.count(ele_in_row)
                mode_ele=ele_in_row
    print(mode_ele)
file1.close()

The error is telling you the problem: you are trying to call split() on a list, but list has no function called split() . I think you want to take a string and split it into a list of lines separated by '\\n' which you could do if you had a string because str has a function named split() . readlines() already does this, so you can just remove the call to split() entirely:

for row_ele in file1.readlines():

If you have used the readline() it became file1 = {'line1', 'line2', ...} . You can only use

for row_ele in file1.readlines():

or

for row_ele in file1.split('\n'):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM