项目数据目录包含许多文件,其文件名以_
开头,如_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
我是这个世界的新手,所以请原谅我的错误。它迫切需要解决查询问题。下次会照顾:) –