简体   繁体   English

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

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

I want to be able to print a number of lines from my file randomly. 我希望能够从我的文件中随机打印多行。

This is my file: 这是我的文件:

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 

This is my current code: 这是我当前的代码:

#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"

Currently, it prints the file in sequential order, ie: 当前,它按顺序打印文件,即:

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): 

However, I would like it to randomly print the lines each time it opens, like this: 但是,我希望它每次打开时都随机打印行,如下所示:

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...

And then the next time the user runs it the questions are in a different order. 然后,下一次用户运行该问题时,问题将以不同的顺序进行。

Any idea how would go about doing this using the random function? 知道如何使用随机函数执行此操作吗?

(I am using Python 2.7) (我正在使用Python 2.7)

You can convert the data from the text into a list and then use the random.choice method from random module to pick a random choice. 您可以将文本中的数据转换为列表,然后使用random模块中的random.choice方法选择随机选择。

Example: 例:

import random

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

print random.choice(data)

One option would be to store the questions in a list and use one of the numpy functions ( numpy.random.permutation ) for the random number generation. 一种选择是将问题存储在列表中,并使用numpy函数之一( numpy.random.permutation )生成随机数。

For example, assuming your questions are stored as-is in the file questions.txt you could do the following: 例如,假设您的问题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