我正在努力完成一项任务,但是有一些困难。我能有人指使我说:
#This worked for me
myFormats = {'audio': ('.wav', '.wma', '.mp3'), 'video': ('.mpg', '.mp4', '.mpeg')}
myFile = '5DeadlyVenoms.mp3'
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
extension = f_exten.next()
使用以下命令导致此错误:
myFormats = {'audio': {'.wav', '.wma', '.mp3'}, 'video': {'.avi', '.mpg', '.mp4', '.mpeg'}}
回溯:
Traceback (most recent call last):
File "C:\Users\GVRSQA004\Desktop\udCombo.py", line 65, in fileFormats
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
TypeError: unsupported operand type(s) for +: 'set' and 'set'
Traceback (most recent call last):
File "C:\Users\GVRSQA004\Desktop\udCombo.py", line 65, in fileFormats
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
TypeError: unsupported operand type(s) for +: 'set' and 'set'
退货
这就是你想要的吗?你知道吗
这是你的错误:
这将始终只返回
myFormats['audio']
,因为这是一个逻辑or
。你想要的是附加的两个元组:更好的解决方案是使用
set
和生成器:你真的想要所有匹配文件扩展名的列表吗?看起来你只是在用第一个。你知道吗
如果没有:
这将允许您轻松地修改它,以便在需要时返回密钥(即音频或视频),或者在需要多个扩展时将其转换为生成器。你知道吗
相关问题 更多 >
编程相关推荐