【问题标题】:How to print a number of lines randomly from a text file (using python)如何从文本文件中随机打印多行(使用python)
【发布时间】:2018-02-07 10:32:09
【问题描述】:

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

这是我的文件:

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

然后当用户下次运行它时,问题的顺序就不同了。

知道如何使用 random 函数来执行此操作吗?

(我使用的是 Python 2.7)

【问题讨论】:

标签: python python-2.7 random text-files


【解决方案1】:

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

示例:

import random

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

print random.choice(data)

【讨论】:

  • 谢谢。我将如何从用户那里获得答案并检查这​​种方法是否正确?
【解决方案2】:

一种选择是将问题存储在一个列表中,并使用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

【讨论】:

  • 谢谢。 Python 2.7 是“numty”吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
相关资源
最近更新 更多