python读取文件每行_python – 在文本文件中的每4行读取,编辑和写入

长时间读者第一次问问.

我正在编写一些我需要编辑时间戳的vtt(隐藏式字幕)文件.该文件的格式如下:

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

我编写了以下代码来读取文件,计算新的时间戳(慢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()

计算效果很好,但问题是它将所有新行转储到文件的开头,而不是写入每个时间戳行并跳过其余的.

我很想听听你们的想法!我已经和它搏斗了一段时间并尝试了很多东西但是还没能完成它.

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值