在调用readlines()
.srt 文件时,我得到了一个包含大量前导和尾随空格的字符列表,如下所示
with open(infile) as f:
r=f.readlines()
return r
我得到了这份清单
['\xef\xbb\xbf1\r\n', '00:00:00,000 --> 00:00:03,000\r\n', "[D. Evans] Now that you've written your first Python program,\r\n",'\r\n', '2\r\n', '00:00:03,000 --> 00:00:06,000\r\n', 'you might be wondering why we need to invent new languages like Python\r\n', '\r\n']
为简洁起见,我只包含了一些元素。如何清理此列表,以便删除所有空白字符并仅获取相关元素,例如
['1','00:00:00,000 --> 00:00:03,000',"[D. Evans] Now that you've written your first Python program"...]