【问题标题】:How do I print one word at a time in python shell?如何在 python shell 中一次打印一个单词?
【发布时间】:2013-12-05 22:10:32
【问题描述】:

我已经有执行此任务的代码,但我只能在命令提示符下执行此任务。你输入函数的数字是每分钟的字数,所以如果你输入 60,你会得到 60 wpm,也就是每秒一个字。

import sys
import time

data = "is sentence with some words this is a some. words this is a sentence with some words".split()


def inputwordsperminute(x):


    max_len=max([len(w) for w in data])
    pad = " "*max_len
    for w in data:
        sys.stdout.write('%s\r' % pad)
        sys.stdout.write("%s\r" % w)
        sys.stdout.flush()
        time.sleep((60.0/x))

print

inputwordsperminute(200)

有人告诉我,我必须导入 curses 才能让它在 shell 中工作。我怎么做?我需要写一些完全不同的东西吗?

【问题讨论】:

  • 您想一次只显示一个单词吗?还是连续的所有单词?
  • 您的代码在 python shell(不带参数运行 python)和 ipython 中非常适合我。当尝试时你会看到什么?
  • 一次只能看到一个单词。该代码在cmd中工作
  • “python shell”是指 Python REPL(当您在没有参数的情况下运行 python 时)?蟒蛇?闲?它适用于我的 REPL。
  • " is sentence with some words this is a some. words 这是一个有一些单词的句子 >>> " 我使用的是 Shell 3.3.2。 IDLE 具体

标签: python shell python-curses


【解决方案1】:

我不确定我是否清楚问题是什么,但只需将您的 \r 更改为 \n 并消除填充可能会提供一些可用的东西而无需诅咒:

#!/usr/local/cpython-2.7/bin/python

import sys
import time

data = "is sentence with some words this is a some. words this is a sentence with some words".split()

def inputwordsperminute(x):
    #max_len=max([len(w) for w in data])
    #pad = " "*max_len
    for w in data:
        #sys.stdout.write('%s\n' % pad)
        sys.stdout.write("%s\n" % w)
        sys.stdout.flush()
        time.sleep((60.0/x))

print

inputwordsperminute(200)

【讨论】:

  • 他想要\r 以产生重击效果。
  • 加 200 wpm?这对 \r 来说太快了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 2017-08-08
  • 2018-04-05
  • 1970-01-01
相关资源
最近更新 更多