简体   繁体   中英

AttributeError: 'list' object has no attribute 'split' -PyTorch

class Flowers(data.Dataset):
def __init__(self, txt, transform=None, target_transform=None, loader=default_loader):
    super(Flowers, self).__init__()
    fh = open(txt, 'r')
    images = []
    for line in fh:
        line = line.strip('\n')
        line = line.rsplit()
        words = line.split()
        words = str(list.split)
        images.append((words[0], int(words[1])))
    self.imgs = images
    self.transform = transform
    self.target_transform = target_transform
    self.loader = loader

As the code above, I am trying to read my picture dataset via a text filled with paths. However, I got a error like this:

File "/Users/paulwang/Library/Application Support/JetBrains/PyCharmCE2020.1/scratches/VGGNet.py", line 130, in <module>
    train_data = Flowers(txt='/Users/paulwang/Desktop/flower_photos_train/train.txt', transform=transform)
  File "/Users/paulwang/Library/Application Support/JetBrains/PyCharmCE2020.1/scratches/VGGNet.py", line 100, in __init__
    words = line.split()
AttributeError: 'list' object has no attribute 'split'

I've gone through some similar questions but I still can't figure out how to solve this problem. I am sincerely looking for help.

The problem is in this pair of statements:

    line = line.rsplit()

line was a string up until now. You just turned it into a list of strings. Therefore, when you try the next line:

    words = line.split()

You're trying an illegal operation. Worse, in the next line, you destroy the result of this second line and replace it with the string representation of the split method.

I strongly recommend that you repeat your tutorial on string processing. Then work through your coding again. After each one or two lines you write, add a print to verify the type and value of the prior statements.

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