如何从文本文件中随机打印多行(使用python)

时间:2018-02-07 10:32:10

标签: python python-2.7 random text-files

我希望能够随机打印我文件中的多行。

这是我的档案:

1. What date did World War II start? 
A. 20th October 1939
B. 1st September 1939

2. Which was a youth organisation group set up by Hitler during WWII for    German youth?
A. Hitler Youth 
B. Edelweiss Pirates 

3. Who succeeded Elizabeth I on the English throne? 
A. Henry VIII
B. James VI 

4. Did Ireland take part in WWII?
A. No
B. Yes 

5.  Who was the Prime Minister of Britain at the start of WWII?
A. Neville Chamberlain 
B. Winston Churchill 

这是我目前的代码:

#Q1
with open ("hisEasyQ.txt") as hisEasyQFile:
    for line in itertools.islice(hisEasyQFile, 0, 3):
        print line
hisEasyA1 = raw_input("Enter your choice (A or B): ").lower()
print "\n"

#Q2
with open ("hisEasyQ.txt") as hisEasyQFile:
    for line in itertools.islice(hisEasyQFile, 4, 7):
        print line
hisEasyA2 = raw_input("Enter your choice (A or B): ").lower()
print "\n"

#Q3
with open ("hisEasyQ.txt") as hisEasyQFile:
    for line in itertools.islice(hisEasyQFile, 8, 11):
        print line
hisEasyA3 = raw_input("Enter your choice (A or B): ").lower()
print "\n"

#Q4
with open ("hisEasyQ.txt") as hisEasyQFile:
    for line in itertools.islice(hisEasyQFile, 12, 15):
        print line
hisEasyA4 = raw_input("Enter your choice (A or B): ").lower()
print "\n"

#Q5
with open ("hisEasyQ.txt") as hisEasyQFile:
    for line in itertools.islice(hisEasyQFile, 16, 19):
        print line
hisEasyA5 = raw_input("Enter your choice (A or B): ").lower()
print "\n"

目前,它按顺序打印文件,即:

1. What date did World War II start? 

A. 20th October 1939

B. 1st September 1939

Enter your choice (A or B): 


2. Which was a youth organisation group set up by Hitler during WWII for German  youth?

A. Hitler Youth 

B. Edelweiss Pirates 

Enter your choice (A or B): 


3. Who succeeded Elizabeth I on the English throne? 

A. Henry VIII

B. James VI 


Enter your choice (A or B): 


4. Did Ireland take part in WWII?

A. No

B. Yes 

Enter your choice (A or B): 


5.  Who was the Prime Minister of Britain at the start of WWII?

A. Neville Chamberlain 

B. Winston Churchill 

Enter your choice (A or B): 

但是,我希望每次打开时随机打印这些行,如下所示:

1. Who was the Prime Minister of Britain at the start of WWII?

A. Neville Chamberlain 

B. Winston Churchill 

Enter your choice (A or B): 

2. Who succeeded Elizabeth I on the English throne? 

A. Henry VIII

B. James VI 


Enter your choice (A or B):

#...etc...

然后,下次用户运行时,问题的顺序也不同。

任何想法如何使用随机函数进行此操作?

(我正在使用Python 2.7)

2 个答案:

答案 0 :(得分:0)

您可以将文本中的数据转换为列表,然后使用随机模块中的 random.choice 方法选择随机选择。

示例:

import random

path = "Path_to"
with open(path, "r") as hisEasyQFile:
    data = hisEasyQFile.read().split("\n\n")

print random.choice(data)

答案 1 :(得分:0)

一种选择是将问题存储在列表中,并使用其中一个numpy函数(numpy.random.permutation)生成随机数。

例如,假设您的问题as-is存储在文件questions.txt中,您可以执行以下操作:

import numpy as np

# Store the questions in a list
#
# Sample format needs to be as-is for each question i.e.:
# Question
# A....
# B....
# blank line
#
questions = []
with open("questions.txt") as fh:
    questions = fh.read().split("\n\n")

# Store the random index and the response as tuples in a list
responses = []
for i in np.random.permutation(range((len(questions)))):
    print questions[i]
    responses.append((i, raw_input("Enter your choice (A or B): ").lower()))

# Print the responses list or process it as needed
print responses