【问题标题】:access column in csv file using for loop使用for循环访问csv文件中的列
【发布时间】:2017-09-12 15:36:29
【问题描述】:

在 SO 上没有什么完全相同的,所以问题是:

我有这个文件结构:(名为 test.csv 的 csv 文件)

1.What line is this?    1
2.What line is this?    2
3.What line is this?    3
4.What line is this?    4
5.What line is this?    5

我希望程序能够打印第一行(1. 这是哪一行?),然后要求用户输入(答案=?),如果答案等于相应列中的内容,则输出“正确”。

目前的代码:

它打印问题并要求答案,但不 1.如果正确则输出“正确” 2. 如果正确,请继续下一个问题

import csv
def main():


    with open('test.csv','r') as f:
        reader=csv.reader(f)
        for row in reader:
                  questions=row
                  print(questions)

                  answer=input("answer:")
                  answer_correct=False

                  for row in reader:
                      for field in row:
                                for i in range(len(questions)):
                                          if field==questions[i]:
                                                        currentindex=questions.index(field)
                                                        if row[currentindex+1]==answer:
                                                                answer_correct==True

    if answer_correct == False :
        print("Wrong answer, sorry!")
    else:
      print("****You're right!*****")

main()

如需解答,请:

  1. 建议对代码进行修复,指出错误并解释解决方案
  2. 建议一个更优雅的修复 /sn-p 以仅使用 csv 阅读器而不使用其他导入工具来解决问题。

如前所述,代码需要: - 提出每个问题,询问用户输入(答案),如果正确或错误(打印正确或错误),然后移动到下一个问题,直到文件结束

到目前为止的输出:

['1.What line is this?', '1']
answer:1
Wrong answer, sorry!
>>>

我也试过这个:

导入 csv

def main():


    with open('test.csv','r') as f:
        reader=csv.reader(f)
        for row in reader:
                  print(row[0])

                  answer=input("answer:")
                  answer_correct=False

                  for row in reader:
                      for field in row:
                                          if answer==row[1]:
                                              answer_correct==True

    if answer_correct == False :
        print("Wrong answer, sorry!")
    else:
      print("****You're right!*****")

main()

【问题讨论】:

  • 请查看更新 - 我可以更好地尝试。虽然仍然不起作用 - 但我觉得我很接近了!

标签: python file csv


【解决方案1】:

我写了小sn-p,很管用。

import csv
with open('questions.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        question, correct_answer = row
        print(question)

        answer = input()
        if answer == correct_answer:
            print("Wow! Su much knowledge!")
        else:
            print("Still trying")

您的代码中似乎有错误的缩进。
并阻止将在整个循环后执行检查您的答案。

【讨论】:

    【解决方案2】:

    假设您的 CSV 文件确实有逗号,如下所示:

    1.What line is this?,1
    2.What line is this?,2
    3.What line is this?,3
    4.What line is this?,4
    5.What line is this?,5
    

    您可以按如下方式对其进行修改:

    import csv
    
    def main():
        with open('test.csv', 'r', newline='') as f_input:
            for question, correct_answer in csv.reader(f_input):
                answer = input(question)
    
                if answer == correct_answer:
                    print("**** You're right! ****")
                else:
                    print("Wrong answer, sorry!")
    
            print("All done")
    
    main()
    

    例如:

    1.What line is this?1
    **** You're right! ****
    2.What line is this?5
    Wrong answer, sorry!
    3.What line is this?7
    Wrong answer, sorry!
    4.What line is this?4
    **** You're right! ****
    5.What line is this?5
    **** You're right! ****
    All done
    

    这使用单个 for 循环来迭代您的问题。如您所见,使用csv.reader() 时,您将逐个遍历条目。每次执行此操作时,您都会推进文件中的位置。在您的代码中,您有两个这样的 for 循环。效果是第一个读取一个条目,下一个 for 循环然后读取更多,它不再从顶部开始。那么问题是第一个现在也继续前进,即它们不是独立的。所以你的主要问题只是有两批for row in reader

    注意:在 Python 3 中,当使用 csv.reader() 时,您应该使用 newline='' 打开文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 2017-03-25
      • 1970-01-01
      • 2018-07-16
      • 2017-09-09
      • 2021-07-13
      • 2013-03-21
      • 2013-10-30
      相关资源
      最近更新 更多