在python中使用文本文件作为列表

时间:2014-12-04 19:37:43

标签: python format text-files

这是更新的程序,在您提供的答案的帮助下。我现在看到并理解如何以成对的方式显示问题和答案,我现在唯一关心的是如何将问题和答案的输出结合起来。例如:

    1. question etc..
    a.answer
    b.answer                 
    c.answer
    d.answer
    e.none of the above

#
#Actual program------
    def main():
        print("Welcome to the Garbology Quiz")
        questionList = getQuestionList()
        answerList = getFormat()
        display = printanswers(questionList,answerList)


    def getQuestionList():
       with open("questions.txt") as questionFile:
            questionList = questionFile.readlines()

       return questionList

    def getFormat():

       formatchoice = input("Would you like the answers printed in metric or american format? (m or     a): ")
       formatchoice = formatchoice.lower()

       while formatchoice != "a" and formatchoice != "m":
          formatchoice = input("Invalid input, please enter a correct value (m or a): ")
          formatchoice = formatchoice.lower()

       if formatchoice == "a":
          answerPath = "american-answers.txt"
       else:
          answerPath = "metric-answers.txt"

       with open(answerPath) as answerFile:
          answerList = answerFile.readlines()

          return answerList

    def printanswers(questionList,answerList):
       for i in range(0,len(questionList)):
          print(questionList[i])
          print(answerList[i])




    main()

3 个答案:

答案 0 :(得分:0)

这很有效,并没有那么臃肿:

def readFile(f):
    fileObj = open(f)
    result = fileObj.read().split("\n")
    fileObj.close()
    return result

questions = readFile("questions.txt")
ameriAnswers = readFile("standard.txt")
metricAnswers = readFile("metric.txt")

while True:
    choice = raw_input("Would you like the answers printed in metric or american format? (m or a): ")
    if choice == 'a':
        for i in range(0, len(questions)): #Assumes the questions file and answer files have the same item count!
            print "Q: ", questions[i]
            print "A: ", ameriAnswers[i]

    elif choice == 'm':
        for i in range(0, len(questions)): #Assumes the questions file and answer files have the same item count!
            print "Q: ", questions[i]
            print "A: ", metricAnswers[i]


    elif choice == 'q':
        break

    else:
        print "Invalid Input!"

    print "\n\n\n\n\n" #Some spacing

如果您只想打印前3个答案,可以使用以下内容替换for循环:

for i in range(0, 3):

请注意,此处没有错误检查,因此如果文件不包含程序所需的内容,则可能会崩溃。

答案 1 :(得分:0)

这与您的代码相当:

# Written for Python 3.

def main():
    print("Welcome to the Garbology Quiz")
    questionList = getQuestionList()
    answerList = getAnswerList()

    print(questionList)
    print(answerList)

def getQuestionList():
   with open("questions.txt") as questionFile:
        questionList = questionFile.readlines()

   return questionList

def getAnswerList():
    formatchoice = input("Would you like the answers printed in metric or american format? (m or a): ")
    formatchoice = formatchoice.lower()

    while formatchoice != "a" and formatchoice != "m":
        formatchoice = input("Invalid input, please enter a correct value (m or a): ")
        formatchoice = formatchoice.lower()

    if formatchoice == "a":
        answerPath = "american-answers.txt"
    else:
        answerPath = "metric-answers.txt"

    with open(answerPath) as answerFile:
        answerList = answerFile.readlines()

    return answerList

main()

答案 2 :(得分:0)

您已将问题和答案读入列表,现在您可以将它们编入索引。一个技巧是首先将它们压缩成对列表,以便稍后迭代更紧凑。 readlines无法移除新行,因此我也会将其删除。

q_and_a = zip((l.strip() for l in questionList), (l.strip() for l in format))
del questionList, format
for i in range(3):
    print("Question %d: %s\nAnswer: %s\n\n" % (i+1, q_and_a[i][0], q_and_a[i][1]))

此外,这种比较并不起作用:

while formatchoice != "a" or formatchoice != "m":

你想'和'他们,还是更好的:

while formatchoice not in ('a', 'm'):