导入两个文本文件以按列表顺序进行比较

时间:2019-09-26 20:09:45

标签: python-3.7

学生试图比较两个选择题a,c,d,b等中的字符串“答案”的.txt文件。我找到了一些有关所遇到问题的不同部分的信息,并发现了一种可能的方法来获取我想要的比较,但该指南仅适用于脚本字符串,而不是从文件中提取列表。

为了导入这两个文件并进行比较,我将代码基于我的教科书和以下视频:Video example

我已经启动并运行了代码,但是由于某种原因,当我想达到100.0%的匹配率时,我只能得到0.0%的匹配率,至少对于我使用的具有相同答案列表的两个文本文件而言。

use std::time::SystemTime;

match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
    Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
    Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}

如果我在两个文本文件的末尾添加close语句,那么我会收到此错误:

  

回溯(最近通话最近):文件   “ c:/ Users / jaret / Documents / Ashford U / CPT 200 / Python Code / Wk 5 Int Assg   -Tester code.py“,第18行,在       answer_sheet.close AttributeError:'str'对象没有属性'close'

1 个答案:

答案 0 :(得分:0)

我不得不重新审视我的文件是如何打开的,并且意识到语法是针对Python 2而不是3的。我选择使用基本打开,后来关闭以减少新手部分的任何潜在错误。 / p>

import difflib

f1 = open('TestAnswerList.txt')
tst_ans = f1.readlines()
f2 = open('StudentAnswerList.txt')
stu_ans = f2.readlines()
sequence = difflib.SequenceMatcher(isjunk=None, a=stu_ans, b=tst_ans)
check_list = sequence.ratio()*100
check_list = round(check_list,1) 
print(str(check_list) + "% match") # Percentage correct
if check_list == 100:
    print('This grade is Plus Ultra!')
elif check_list >= 75:
    print('Good job, you pass!')
else:
    print('Please study harder for your next test.')
# Visual Answer match-up
print('Test Answers:   ', tst_ans)
print('Student Answers:', stu_ans)

f1.close()
f2.close()