[英]writing time equal number of line in file text
I want to write time in every line in front of Text by check number of line in text file. 我想通过检查文本文件中的行数在文本前面的每一行中写时间。 Because I want to convert this text file to .srt file by this link.
因为我想通过此链接将此文本文件转换为.srt文件。 https://unix.stackexchange.com/questions/111024/how-to-convert-a-txt-subtitle-file-to-srt-format
https://unix.stackexchange.com/questions/111024/how-to-convert-a-txt-subtitle-file-to-srt-format
Every one line is 1 second.(start first line at 00:00:00:) My file has many line (500-1000 line up) from 每一行是1秒。(从00:00:00开始第一行:)我的文件中有多行(从500-1000行)
.......Text......... (first line)
.......Text......... (second line)
.......Text......... (third line)
.
.
.
.......Text......... (60 line)
.......Text......... (61 line)
.
.
.................... (3600 line)
to this 对此
00:00:01:.......Text......... (first line)
00:00:02:.......Text......... (second line)
00:00:03:.......Text......... (third line)
.
.
.
00:01:00:.......Text......... (60 line)
00:01:01:.......Text......... (61 line)
.
.
01:00:00:.................... (3600 line)
Now I can check number of line and write text in front of text every line. 现在,我可以检查行数并在每行文本的前面写文本。 But error when use code check numberline and open file write text.
但是使用代码检查数字行并打开文件写入文本时出错。 I don't know how to write for loop and compare number of line.
我不知道如何编写循环并比较行数。 I'm noob for this.
我对此不感兴趣。
def numberline(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
numberline("/home/pi/gtest.txt"))
dataFile = open("/home/pi/gtest.txt", "w")
for line in range(numberline("/home/pi/gtest.txt")):
dataFile.write("00:00:%02d:\n" % line)
dataFile.close()
Please help me. 请帮我。 Thankyou This my project wait for this part because this is last part in python.
谢谢这是我的项目等待这一部分,因为这是python中的最后一部分。 Normally I use Rpi with linux command but not understand in python.
通常我在Linux命令中使用Rpi,但在python中却不了解。
Try this: 尝试这个:
import datetime
str(datetime.timedelta(seconds=666))
result = ''
countTime = 0
for line in open('/home/pi/gtest.txt'):
result = result + (str(datetime.timedelta(seconds=countTime)) + ":" + line) + '\n'
countTime = countTime+1
print (result)
You can then just write the result to the file: 然后,您可以将结果写入文件中:
with open('/home/pi/gtest.txt', 'w') as the_file:
the_file.write(result)
You can use the enumerate()
function to get the line number and the line from a file when iterating, and divmod()
to divide-and-remainder a number. 您可以使用
enumerate()
函数在进行迭代时从文件中获取行号和行,并可以使用divmod()
来divmod()
并divmod()
数字。 Here we do it twice, first to get from just seconds (the line number) to minutes and seconds, then from minutes to hours and minutes. 在这里,我们做了两次,首先是从几秒钟(行号)到几分钟和几秒钟,然后是几分钟到几小时和几分钟。
Then, we use regular %
interpolation with the %02d
specifier to turn the hours/minutes/seconds variables into zero-padded two-character-length strings, and glue them together with colons to the original line. 然后,我们将常规
%
插值与%02d
说明符一起使用,以将小时/分钟/秒变量转换为零填充的两个字符长度的字符串,并将其与冒号粘合到原始行。
For added meta points, this script annotates itself by default. 对于添加的元点,默认情况下,此脚本自行注释。 :)
:)
To write the output to another file, it's easiest to just do it using the shell: 要将输出写入另一个文件,最简单的方法是使用shell:
$ python this-script.py > annotated.txt
This is Python 3, by the way; 顺便说一下,这是Python 3。 the
print()
statement would look different on Python 2. 在Python 2上,
print()
语句看起来会有所不同。
EDIT: Added argument parsing as per discussion in comments. 编辑:根据注释中的讨论添加了参数解析。 The script now allows
该脚本现在允许
$ python this-script.py inputfile.txt > annotated.txt
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('input', default=__file__)
args = ap.parse_args()
with open(args.input, 'r') as f:
for line_number, line in enumerate(f):
minutes, seconds = divmod(line_number, 60)
hours, minutes = divmod(minutes, 60)
print('%02d:%02d:%02d:%s' % (hours, minutes, seconds, line), end='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.