Search code examples
pythonpandasattributeerror

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


Here is my code for using the read_pcikle function from pandas. It can work for a single document.

import pandas as pd
tt = './data_v6/level3/6954/tweets_text_Kea.txt'
pd = pd.read_pickle(tt)
print(type(pd))
f = open("./data_v6/level3/6954/tweets_KeaWC.txt",'a',encoding='utf-8')
for i in pd:
         #print(i)
    #print(type(i))
    for j in i:
        res = isinstance(j, str)
        if res == True:
               print(j)
               f.write(j+'\n')

Then I try to use this function in all the files I have in my data. But I got an error: AttributeError: 'list' object has no attribute 'read_pickle'.

And here is the code for all files:

import pandas as pd
import os
count = 1
for root, dirs, files in os.walk("./data_v6/level3/"):
    for dir in dirs:
        print(count)
        #print(dir)
        count += 1
        tt = './data_v6/level3/'+dir+'/tweets_text_Kea.txt'
        print(tt)
        pd = pd.read_pickle(tt)
        print(type(pd))
        f = open("./data_v6/level3/"+dir+"/tweets_KeaWC.txt",'a',encoding='utf-8')
        for i in pd:
         #print(i)
    #print(type(i))
         for j in i:
            res = isinstance(j, str)
            if res == True:
               #print(j)
               f.write(j+'\n')

Solution

  • Well, look what you are doing with pandas:

    import pandas as pd    # pd -> pandas
    ...
    pd = pd.read_pickle(tt)    # pd -> list 
    

    You may not assign your read_pickle to pd; otherwise you cannot call pandas methods with pd anymore! With your code, you call pd once before you reassign pd and thus the next time you call pd it does not work anymore!

    Use this code instead:

    import pandas as pd
    import os
    
    for root, dirs, files in os.walk("./data_v6/level3/"):
        for dir in dirs:
            tt = './data_v6/level3/'+dir+'/tweets_text_Kea.txt'
            pickle_output = pd.read_pickle(tt)
            f = open("./data_v6/level3/"+dir+"/tweets_KeaWC.txt",'a',encoding='utf-8')
            for i in pickle_output:
                for j in i:
                    res = isinstance(j, str)
                    if res == True:
                        f.write(j+'\n')
            f.close()