簡體   English   中英

AttributeError: 'list' object 沒有屬性 'split' -PyTorch

[英]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

正如上面的代碼,我試圖通過一個充滿路徑的文本來讀取我的圖片數據集。 但是,我收到這樣的錯誤:

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'

我已經經歷了一些類似的問題,但我仍然無法弄清楚如何解決這個問題。 我真誠地尋求幫助。

問題出在這對語句中:

    line = line.rsplit()

到目前為止, line是一個字符串。 你只是把它變成了一個字符串列表。 因此,當您嘗試下一行時:

    words = line.split()

你正在嘗試非法操作。 更糟糕的是,在下一行中,您破壞了第二行的結果並將其替換為split方法的字符串表示形式。

我強烈建議您重復有關字符串處理的教程。 然后再次完成您的編碼。 在您編寫的每一行或兩行之后,添加print以驗證先前語句的類型和值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM