为什么我的对象一直变成 NoneType?
Why does my object keep turning in to a NoneType?
我正在构建一个 Youtube 音频采样程序,我设置了一个名为 time_snip 的对象。
包含样本开始和结束的对象(以毫秒为单位)。
我注意到,如果我输入的时间正确,我会得到预期的输出,但是如果我以错误的格式输入时间(try/except 异常集),然后我以正确的方式再次输入,我会得到一个AttributeError: 'NoneType' object has no attribute 'start'
time_snip class:
class time_snip:
def __init__(self,start_hour,start_min,start_sec,start_msec,end_hour,end_min,end_sec,end_msec):
self.start = (start_hour*60*60*1000)+(start_min * 60 * 1000) + (start_sec * 1000)+(start_msec) # Time to milliseconds
self.end = (end_hour*60*60*1000)+(end_min * 60 * 1000) + (end_sec * 1000)+(end_msec) # Time to milliseconds
self.start_ind = (str(start_min)+':'+str(start_sec))
self.end_ind =(str(end_min)+':'+str(end_sec))
主要代码:
from pytube import YouTube
from TimeSnip import time_snip
time_options = input("Would you like to find by time(1) or by word(2)?: ")
url ="https://www.youtube.com/watch?v=vGwNPeEXEDM&ab_channel=SilasLaspada"
ytd = YouTube(URL)
def time_set(youtube_object):
try:
hr_length = int(ytd.length/3600)
min_length = int((ytd.length%3600)/60)
sec_length = ytd.length%60
print("the video is " + str(hr_length)+':'+str(min_length)+':'+str(sec_length) +'long')
start_time_srt_format = input("Enter start time in 'hr:min:sec,msec' format: ")
end_time_srt_format = input("Enter end time in 'hr:min:sec,msec' format: ")
start_parts = start_time_srt_format.split(':')
end_parts = end_time_srt_format.split(':')
start_secmsec = start_parts[2].split(',')
end_secmsec = end_parts[2].split(',')
start_hr = int(start_parts[0])
start_min = int(start_parts[1])
start_sec = int(start_secmsec[0])
start_msec = int(start_secmsec[1])
end_hr = int(end_parts[0])
end_min = int(end_parts[1])
end_sec = int(end_secmsec[0])
end_msec = int(end_secmsec[1])
word = (str(start_sec)+'-'+str(end_sec))
timeSnip = time_snip(start_hr, start_min, start_sec, start_msec, end_hr, end_min,end_sec,end_msec)
return timeSnip
except IndexError:
print("Invalid input! Try again!")
time_set(youtube_object)
if time_options =='1':
timeSnip = time_set(ytd)
word = str(timeSnip.start/1000)+' sec'
elif time_options =='2':
word = input("Enter word: ")
我不知道为什么会这样。
在 except 子句中,您不需要 return 任何东西。因此,如果异常触发,您将再次调用该函数(这不是正确的方法),但即使成功并且 return 是一个值,您也将其丢弃。将该行更改为 return time_set(youtube_object)
,但最好将其设为 while 循环而不是递归调用。
我正在构建一个 Youtube 音频采样程序,我设置了一个名为 time_snip 的对象。
包含样本开始和结束的对象(以毫秒为单位)。
我注意到,如果我输入的时间正确,我会得到预期的输出,但是如果我以错误的格式输入时间(try/except 异常集),然后我以正确的方式再次输入,我会得到一个AttributeError: 'NoneType' object has no attribute 'start'
time_snip class:
class time_snip:
def __init__(self,start_hour,start_min,start_sec,start_msec,end_hour,end_min,end_sec,end_msec):
self.start = (start_hour*60*60*1000)+(start_min * 60 * 1000) + (start_sec * 1000)+(start_msec) # Time to milliseconds
self.end = (end_hour*60*60*1000)+(end_min * 60 * 1000) + (end_sec * 1000)+(end_msec) # Time to milliseconds
self.start_ind = (str(start_min)+':'+str(start_sec))
self.end_ind =(str(end_min)+':'+str(end_sec))
主要代码:
from pytube import YouTube
from TimeSnip import time_snip
time_options = input("Would you like to find by time(1) or by word(2)?: ")
url ="https://www.youtube.com/watch?v=vGwNPeEXEDM&ab_channel=SilasLaspada"
ytd = YouTube(URL)
def time_set(youtube_object):
try:
hr_length = int(ytd.length/3600)
min_length = int((ytd.length%3600)/60)
sec_length = ytd.length%60
print("the video is " + str(hr_length)+':'+str(min_length)+':'+str(sec_length) +'long')
start_time_srt_format = input("Enter start time in 'hr:min:sec,msec' format: ")
end_time_srt_format = input("Enter end time in 'hr:min:sec,msec' format: ")
start_parts = start_time_srt_format.split(':')
end_parts = end_time_srt_format.split(':')
start_secmsec = start_parts[2].split(',')
end_secmsec = end_parts[2].split(',')
start_hr = int(start_parts[0])
start_min = int(start_parts[1])
start_sec = int(start_secmsec[0])
start_msec = int(start_secmsec[1])
end_hr = int(end_parts[0])
end_min = int(end_parts[1])
end_sec = int(end_secmsec[0])
end_msec = int(end_secmsec[1])
word = (str(start_sec)+'-'+str(end_sec))
timeSnip = time_snip(start_hr, start_min, start_sec, start_msec, end_hr, end_min,end_sec,end_msec)
return timeSnip
except IndexError:
print("Invalid input! Try again!")
time_set(youtube_object)
if time_options =='1':
timeSnip = time_set(ytd)
word = str(timeSnip.start/1000)+' sec'
elif time_options =='2':
word = input("Enter word: ")
我不知道为什么会这样。
在 except 子句中,您不需要 return 任何东西。因此,如果异常触发,您将再次调用该函数(这不是正确的方法),但即使成功并且 return 是一个值,您也将其丢弃。将该行更改为 return time_set(youtube_object)
,但最好将其设为 while 循环而不是递归调用。