在Python中打印动态和大型列表

时间:2018-01-11 17:15:21

标签: python list printing delay subtitle

我正在尝试打印一个大型列表,如电视中的一系列租船人或副标题。我已附上以下照片以了解更多信息:

enter image description here

正如您所看到的那样,输入文本应该从右向左旋转。

为了达到这一点,我编写了以下代码:

from __future__ import print_function
import os
from time import sleep

text = """A company which supplied lingerie to the Queen has lost its royal warrant over a book which revealed details of royal bra fittings.Rigby & Peller,
a luxury underwear firm founded in London, had held the royal warrant since 1960. It was withdrawn after June Kenton, who fitted bras for the Quee
n, released a book called 'Storm in a D-Cup'.Mrs Kenton said there was "nothing" in the book to "be upset about", adding that it was an "unbelieva
ble" decision.Buckingham Palace said it did not comment on individual companies"""


whole_Str = []
i = 0
while(i < len(text)):
    rest = text[i:i+50]
    i+=1
    whole_Str.append(rest)
for sequence in whole_Str:
    print (sequence, end ='\r')
    sleep(0.1)

然而,有两个问题:

首先,python编辑器不允许我在同一个地方覆盖此文本的不同部分。

其次,最重要的是,我认为sleep()不是创建延迟的合理解决方案,因为对于大型文本,系统不应该停止只打印字幕。

非常感谢任何形式的帮助

2 个答案:

答案 0 :(得分:0)

也许你的第一个问题就解决了:

import sys
from time import sleep

text = """dummy text"""    

whole_Str = text

for i in range(len(text)-50):
    sys.stdout.write("\r" + text[i:i+50])
    sys.stdout.flush()
    sleep(0.1)

不确定你对第二个的意思。

答案 1 :(得分:0)

我能够使用curses库。代码看起来像这样。

import time
import curses

text = """A company which supplied lingerie to the Queen has lost its royal warrant over a book which revealed details of royal bra fittings.Rigby & Peller,
a luxury underwear firm founded in London, had held the royal warrant since 1960. It was withdrawn after June Kenton, who fitted bras for the Quee
n, released a book called 'Storm in a D-Cup'.Mrs Kenton said there was "nothing" in the book to "be upset about", adding that it was an "unbelieva
ble" decision.Buckingham Palace said it did not comment on individual companies"""

def pbar(window):
    for i in text:
        #add a character to the string
        window.addstr(i)

        #refresh the window so the new character appears
        window.refresh()

        #sleep to get the scroll effect
        time.sleep(0.5)

#start the printing
curses.wrapper(pbar)

这样可以获得从左向右滚动的水平滚动文本的效果。