簡體   English   中英

如何從文本文件中隨機打印多行(使用python)

[英]How to print a number of lines randomly from a text file (using python)

我希望能夠從我的文件中隨機打印多行。

這是我的文件:

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)

您可以將文本中的數據轉換為列表,然后使用random模塊中的random.choice方法選擇隨機選擇。

例:

import random

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

print random.choice(data)

一種選擇是將問題存儲在列表中,並使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM