2024-10-01 13:39:33 发布
网友
以下是我正在执行的代码
https://github.com/federico-terzi/gesture-keyboard/blob/master/learn.py
在执行我得到的代码之后
“文件”学习.py“,第57行,英寸
number = ord(category) -ord('a')
TypeError: ord() expected a character, but string of length 0 found
我怎样才能修好它?在
查看链接到的代码,category来自
category
category = name.split("_")[0]
并且name来自:
name
所以我猜你有一个带前导下划线的文件名。将此字符串拆分到'_'这将为列表的第一个值提供一个空字符串。示例:
'_'
s = '_abc_test.txt' s.split('_') # returns: ['', 'abc', 'test.txt']
它的第零个元素是一个空字符串,它被传递给ord。在
ord
项目数据目录包含许多文件,其文件名以_开头,类似于_sample_t10_34.txt。在
_
_sample_t10_34.txt
所以在你的代码里
for path, subdirs, files in os.walk(root): for name in files: category = name.split("_")[0] # here category = ''
现在下一行是:
在这里,^{}接受长度为1的str类型的参数,您会得到这个错误,因为当名称为_sample_t10_34.txt的文件被ign read时,category有时会是一个空字符串''。在
str
''
您可以跳过以_开头的文件,方法是使用if statement检查该文件是否以_开头。在
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
来自并且
^{pr2}$name
来自:所以我猜你有一个带前导下划线的文件名。将此字符串拆分到
'_'
这将为列表的第一个值提供一个空字符串。示例:它的第零个元素是一个空字符串,它被传递给
ord
。在项目数据目录包含许多文件,其文件名以
_
开头,类似于_sample_t10_34.txt
。在所以在你的代码里
现在下一行是:
^{pr2}$在这里,^{} 接受长度为1的
str
类型的参数,您会得到这个错误,因为当名称为_sample_t10_34.txt
的文件被ign read时,category有时会是一个空字符串''
。在您可以跳过以
_
开头的文件,方法是使用if statement
检查该文件是否以_
开头。在相关问题 更多 >
编程相关推荐