Python “类型错误:字节索引必须是整数或切片,而不是 str”将字节转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33773806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"TypeError: byte indices must be integers or slices, not str" Converting bytes to ints
提问by Lewis Menelaws
I am using a different program (ffmpeg) to grab the length of a youtube video that was downloaded in order to randomize a specific point in the video. However I am getting this error when I am trying to execute this code:
我正在使用不同的程序 (ffmpeg) 来获取下载的 youtube 视频的长度,以便随机化视频中的特定点。但是,当我尝试执行此代码时出现此错误:
def grabTimeOfDownloadedYoutubeVideo(youtubeVideo):
process = subprocess.Popen(['/usr/local/bin/ffmpeg', '-i', youtubeVideo], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = str(re.search(b"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()).encode()
print(matches)
hours = int(matches['hours'])
minutes = int(matches['minutes'])
seconds = int(matches['seconds'])
total = 0
total += 60 * 60 * hours
total += 60 * minutes
total += seconds
print(total)
The matches variable prints out to this:
匹配变量打印出来:
b"{'minutes': b'04', 'hours': b'00', 'seconds': b'24.94'}"
So all of the output comes out with a 'b' at the beginning of it. How do I remove the 'b' and just get the number?
所以所有的输出都以'b'开头。如何删除“b”并获取数字?
Full error message here:
完整的错误信息在这里:
Traceback (most recent call last):
File "bot.py", line 87, in <module>
grabTimeOfDownloadedYoutubeVideo("videos/1.mp4")
File "bot.py", line 77, in grabTimeOfDownloadedYoutubeVideo
hours = int(matches['hours'])
TypeError: byte indices must be integers or slices, not str
采纳答案by Tim Pietzcker
matches = str(re.search(b"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()).encode()
is weird. By converting the result of the regex match to a string, you're causing the error (because now matches['hours']
will fail).
很奇怪。通过将正则表达式匹配的结果转换为字符串,您会导致错误(因为现在matches['hours']
将失败)。
By encoding that string to a bytes
object (why?), you're complicating things even further.
通过将该字符串编码为一个bytes
对象(为什么?),您将事情进一步复杂化。
matches = re.search(r"Duration:\s(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout).groupdict()
should do (although I'm uncertain about using stdout
as input...)
应该做(虽然我不确定stdout
用作输入......)
回答by Dimitris Fasarakis Hilliard
It seems you have a byte
object there. In order to use it you can do the following**:
看来你byte
那里有一个对象。为了使用它,您可以执行以下操作**:
Decode it:
解码它:
matches = matches.decode("utf-8")
Then, by using ast.literal_eval
, translate the str
to what it truly is, a dict
:
然后,通过使用ast.literal_eval
,将 转换str
为它的真正含义, a dict
:
matches = ast.literal_eval(matches)
Then you can access the contents of matches as you normally would:
然后您可以像往常一样访问匹配项的内容:
int(matches['hours']) # returns 0
**Of course this simply fixes an error that really shouldn't be here in the first place as @Tim points out.
**当然,这只是修复了一个错误,正如@Tim 指出的那样,该错误一开始就不应该出现在这里。