选择一个文件并从中读取python中的单词

时间:2016-09-13 17:57:17

标签: python

我需要帮助,我是python的初学者。我的任务是创建一个程序,让用户选择一个类别,然后从该类别的文件中加扰单词。我只是想弄清楚为什么第一部分不起作用,第一部分是根据用户选择的类别运行的四种不同方法中的第一部分。

public class RetryWithDelay implements 
                            Func1<Observable<? extends Throwable>, Observable<?>> {

    private final int MAX_RETRIES;
    private final int DELAY_DURATION;
    private final int START_RETRY;

    /**
     * Provide number of retries and seconds to be delayed between retry.
     *
     * @param maxRetries             Number of retries.
     * @param delayDurationInSeconds Seconds to be delays in each retry.
     */
    public RetryWithDelay(int maxRetries, int delayDurationInSeconds) {
        MAX_RETRIES = maxRetries;
        DELAY_DURATION = delayDurationInSeconds;
        START_RETRY = 1;
    }

    @Override
    public Observable<?> call(Observable<? extends Throwable> observable) {
        return observable
                .delay(DELAY_DURATION, TimeUnit.SECONDS)
                .zipWith(Observable.range(START_RETRY, MAX_RETRIES), 
                         new Func2<Throwable, Integer, Integer>() {
                             @Override
                             public Integer call(Throwable throwable, Integer attempt) {
                                  return attempt;
                             }
                         });
    }
}

1 个答案:

答案 0 :(得分:2)

第一个问题是你只能从文件中读取1-5个字母。 请阅读(文档)[https://docs.python.org/2/tutorial/inputoutput.html],了解读取功能的工作原理。括号中的数字是您要读取的字节数。

您可能需要一个更简单的解决方案,例如读取整个文件并将其拆分为单词。这看起来像是:

file_contents = animals.read()
animalList = file_contents.split()

如果拆分对您来说是新的,那么(查询)[https://docs.python.org/2/library/string.html]该方法也是如此。

下一个问题是您已将动物列表设置为文字字符串,而不是您读取的输入值。我想你想要这一行:

animalList = [animal1, animal2, animal3, animal4, animal5]