'地图'对象不可下标

问题描述 投票:0回答:1

我正试图执行这个链接中的代码。https:/zulko.github.ioblog20140621some-more-videogreping-with-python。

import re # module for regular expressions

def convert_time(timestring):
    """ Converts a string into seconds """
    nums = map(float, re.findall(r'\d+', timestring))
    return 3600*nums[0] + 60*nums[1] + nums[2] + nums[3]/1000

with open("Identity_2003.srt") as f:
    lines = f.readlines()

times_texts = []
current_times , current_text = None, ""
for line in lines:
    times = re.findall("[0-9]*:[0-9]*:[0-9]*,[0-9]*", line)
    if times != []:
        current_times = map(convert_time, times)
    elif line == '\n':
        times_texts.append((current_times, current_text))
        current_times, current_text = None, ""
    elif current_times is not None:
        current_text = current_text + line.replace("\n"," ")

print (times_texts)


from collections import Counter
whole_text = " ".join([text for (time, text) in times_texts])
all_words = re.findall("\w+", whole_text)
counter = Counter([w.lower() for w in all_words if len(w)>5])
print (counter.most_common(10))

cuts = [times for (times,text) in times_texts
        if (re.findall("please",text) != [])]



from moviepy.editor import VideoFileClip, concatenate

video = VideoFileClip("Identity_2003.mp4")

def assemble_cuts(cuts, outputfile):
    """ Concatenate cuts and generate a video file. """
    final = concatenate([video.subclip(start, end)
                         for (start,end) in cuts])
    final.to_videofile(outputfile)

assemble_cuts(cuts, "please.mp4")

但是 assemble_cuts 函数不工作。我使用的是python3.x

它给我一个错误

Traceback (most recent call last):

  File "<ipython-input-64-939ee3d73a4a>", line 47, in <module>
    assemble_cuts(cuts, "please.mp4")

  File "<ipython-input-64-939ee3d73a4a>", line 44, in assemble_cuts
    for (start,end) in cuts])

  File "<ipython-input-64-939ee3d73a4a>", line 43, in <listcomp>
    final = concatenate([video.subclip(start, end)

  File "<ipython-input-64-939ee3d73a4a>", line 6, in convert_time
    return 3600*nums[0] + 60*nums[1] + nums[2] + nums[3]/1000

TypeError: 'map' object is not subscriptable

你能帮我解决这个问题吗?

python-3.x video video-processing moviepy
1个回答
0
投票

修正了。

def convert_time(timestring):
    """ Converts a string into seconds """
    nums = list(map(float, re.findall(r'\d+', timestring)))
    return 3600*nums[0] + 60*nums[1] + nums[2] + nums[3]/1000
© www.soinside.com 2019 - 2024. All rights reserved.