0

我将尾随的 print() 方法放在代码末尾的 write() 方法行旁边,以测试为什么我的输出文件不完整。但是, print() 输出是我期望的“所有东西”;而 write() 输出的关闭量令人困惑(200 个“事物”中只有 150 个)。输出的参考图像IDLE 与外部输出文件

仅供参考:Win 7 64 // Python 3.4.2

我的模块采用 SRT 字幕文件('test.srt')并返回我从中创建的列表对象;特别是一个包含 220 个列表条目的表单:[[(index), [time], string]]

times = open('times.txt', 'w')

### A portion of Riobard's SRT Parser: srt.py
import re

def tc2ms(tc):
    ''' convert timecode to millisecond '''

    sign    = 1
    if tc[0] in "+-":
        sign    = -1 if tc[0] == "-" else 1
        tc  = tc[1:]

    TIMECODE_RE     = re.compile('(?:(?:(?:(\d?\d):)?(\d?\d):)?(\d?\d))?(?:[,.](\d?\d?\d))?')
    match   = TIMECODE_RE.match(tc)
    try: 
        assert match is not None
    except AssertionError:
        print(tc)
    hh,mm,ss,ms = map(lambda x: 0 if x==None else int(x), match.groups())
    return ((hh*3600 + mm*60 + ss) * 1000 + ms) * sign

# my code
with open('test.srt') as f:
    file = f.read()

srt = []

for line in file:
    splitter = file.split("\n\n")

# SRT splitter
i = 0
j = len(splitter)
for items in splitter:
    while i <= j - 2:
        split_point_1 = splitter[i].index("\n")
        split_point_2 = splitter[i].index("\n", split_point_1 + 1)
        index = splitter[i][:split_point_1]
        time = [splitter[i][split_point_1:split_point_2]]
        time = time[0][1:]
        string = splitter[i][split_point_2:]
        string = string[1:]
        list = [[(index), [time], string]]
        srt += list
        i += 1

# time info outputter
i = 0
j = 1
for line in srt:
    if i != len(srt) - 1:
        indexer = srt[i][1][0].index(" --> ")
        timein = srt[i][1][0][:indexer]
        timeout = srt[i][1][0][-indexer:]
        line_time = (tc2ms(timeout) - tc2ms(timein))/1000
        space_time = ((tc2ms((srt[j][1][0][:indexer]))) - (tc2ms(srt[i][1][0][-indexer:])))/1000
        out1 = "The space between Line " + str(i) + " and Line " + str(j) + " lasts " + str(space_time) + " seconds." + "\n"
        out2 = "Line " + str(i) + ": " + str(srt[i][2]) + "\n\n"
        times.write(out1)
        times.write(out2)
        print(out1, end="")
        print(out2)
        i += 1
        j += 1
    else:
        indexer = srt[i][1][0].index(" --> ")
        timein = srt[i][1][0][:indexer]
        timeout = srt[i][1][0][-indexer:]
        line_time = (tc2ms(timeout) - tc2ms(timein))/1000
        outend = "Line " + str(i) + ": " + str(srt[i][2]) + "\n<End of File>"
        times.write(outend)
        print(outend)

我的两个 write() 方法输出文件分别只打印出 220 项中的 150 或 200 项,否则它会正确打印到屏幕上。

4

1 回答 1

1

您想在完成写入后关闭文件;times操作系统使用写缓冲区来加速文件 I/O,一次性收集更大的数据块写入磁盘;关闭文件会刷新该缓冲区:

times.close()

with考虑在一个块中打开文件:

with open('times.txt', 'w') as times:
    # all code that needs to write to times
于 2015-05-07T09:06:54.360 回答