項目數據目錄包含許多文件,其文件名以_
開頭,如_sample_t10_34.txt
。
因此,在你的代碼
for path, subdirs, files in os.walk(root):
for name in files:
category = name.split("_")[0] # here category = ''
現在的下一行是:
number = ord(category) - ord("a")
這裏ord()
需要長度爲1 str
類型的參數,你得到這個錯誤,因爲類別將某個是一個空字符串''
,當名稱爲_sample_t10_34.txt
的文件被讀取時。
你可以做的是跳過以_
開頭的文件,通過檢查if statement
文件是否不以_
開頭。
for path, subdirs, files in os.walk(root):
for name in files:
if not name.startswith('_'):
# code here after if statement
category = name.split("_")[0]
number = ord(category) - ord("a")
# rest of code..
通過將東西放入'category'中,這似乎是一個空列表。 – GPhilo
另外,請閱讀[如何寫好問題的指導方針](https://stackoverflow.com/help/how-to-ask),代碼應該都在問題中,應避免與github的鏈接(因爲他們可能隨時破壞) – GPhilo
我是這個世界的新手,所以請原諒我的錯誤。它迫切需要解決查詢問題。下次會照顧:) –