[英]Reading, editing, and writing over every 4th line in a text file
long time reader first time asker. 长时间读者第一次问问。
I am working on some vtt (closed caption) files that I need to edit the timestamps for. 我正在编写一些我需要编辑时间戳的vtt(隐藏式字幕)文件。 The format of the file is as follows:
该文件的格式如下:
177
00:07:37.450 --> 00:07:39.690
- [Liz] How would you suggest an organization devise
178
00:07:39.690 --> 00:07:41.719
the accountabilities for culture?
179
00:07:41.719 --> 00:07:43.690
- [Tamara] It is a shared accountability
I have written the following code to read the file, calculate the new timestamps (5% slower) and spit out the new timestamps: 我编写了以下代码来读取文件,计算新的时间戳(慢5%)并吐出新的时间戳:
from sys import argv
script, filename = argv
adjustment = input("Adjustment multiplier: ")
video = open(filename, "r+")
lines = video.readlines()
video.seek(0)
for l in lines:
if l[:2] == "00":
#here I've omitted a lot of calculations to turn the timestamps
#into milliseconds, apply the adjustment multiplier, and turn them back into
#minutes, seconds, and milliseconds.
new_line = str(#concatenation of new values into timestamp format)
video.write(new_line)
video.close()
The calculations work great, but the problem is that it dumps all the new lines into the start of the file instead of writing over each timestamp line and skipping the rest. 计算效果很好,但问题是它将所有新行转储到文件的开头,而不是写入每个时间戳行并跳过其余的。
I would love to hear what you guys think! 我很想听听你们的想法! I've been wrestling with this for a while and have tried a bunch of things but haven't quite been able to make it work.
我已经和它搏斗了一段时间并尝试了很多东西但是还没能完成它。
Thank you! 谢谢!
Fixed it! 固定它!
All I had to do was add: 我所要做的就是添加:
else:
video.write(l)
to the if-statement. 到if语句。 That way, if it matches my parameters the calculations are run and it writes the new line, but if it doesn't, it just writes the old line.
这样,如果它匹配我的参数,则运行计算并写入新行,但如果不匹配,则只写入旧行。
Thanks all! 谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.