[英]How do I compare a record in a csv (txt) file to a record in another csv (txt) file and calculate a number based on their comparison?
I am trying to compare words in a csv (txt) file to those in another csv (txt) file using Python. 我正在尝试使用Python将一个csv(txt)文件中的单词与另一个csv(txt)文件中的单词进行比较。 I am then trying to calculate a score based on their similarity. 然后,我试图基于它们的相似性来计算分数。 One file (Answer.txt) contains answers for a test. 一个文件(Answer.txt)包含测试答案。 The other (Test.txt) contains both the definitions and answers of the test. 另一个(Test.txt)包含测试的定义和答案。 The problem is that I am fairly new to coding and that both files have a different layout. 问题是我对编码还很陌生,并且两个文件的布局都不同。 For example, Answers.txt has a layout like this: 例如,Answers.txt具有如下布局:
dog,cat,sheep
Test.txt has a layout like this: Test.txt的布局如下:
definition_1,def_2,def_3
dog,cat,sheep
For every row in Answer.txt I am trying to compare each word to the last row in Test.txt. 对于Answer.txt中的每一行,我试图将每个单词与Test.txt中的最后一行进行比较。 I am also trying to calculate a score based on the matching words. 我也在尝试根据匹配的单词来计算分数。 If a word matches, I would like 2 marks to be awarded. 如果一个单词匹配,我希望获得2分。 For example: 例如:
If the answers in Answer.txt say: 如果Answer.txt中的答案是:
dog,cat,sheep
And the answers in Test.txt say: Test.txt中的答案是:
dog,cat,sheep
... A score of 6 will be calculated. ...将会得到6分。 However, if the answers in Answers.txt say: 但是,如果Answers.txt中的答案是:
dog cat,shep
... Then 4 points will be awarded, because 'sheep' does not match Test.csv. ...然后将获得4分,因为“绵羊”与Test.csv不匹配。
I would then like this 'score' to be added to the end of a record in another file called 'Score.txt'. 然后,我希望将此“分数”添加到另一个名为“ Score.txt”的文件的记录末尾。
Sorry for the trouble. 抱歉,添麻烦了。 This is just about as simplified as I can make my explanation. 就我的解释而言,这简直就是简化。 You're help will be massively appreciated! 您的帮助将不胜感激!
Added: 添加:
I have no idea where to start. 我不知道从哪里开始。 I have, however, written the function which stores the user's input in Answers.txt: 但是,我已经编写了将用户输入内容存储在Answers.txt中的函数:
def ask_question(number, definition):
""" Asks a question for definition number """
print "Definition {}: {}".format(number, definition)
return raw_input("Answer: ")
def sit_a_test():
results = []
with open("Test.txt", "rb") as f:
for row in csv.reader(f):
results.append(list(ask_question(no, def) for no, def in enumerate(row))))
with open("Score.txt", "wb") as f:
writer = csv.writer(f)
writerow.writerows(results)
You can try something like this, using numpy 您可以尝试使用numpy这样的事情
import numpy as np
# Load the files:
answers = np.loadtxt(open("Answer.txt"), dtype='str', delimiter=",")
test = np.loadtxt(open("Text.txt"), dtype='str', delimiter=",")
# Compare
scores = 2 * np.sum(answers == test, axis=0)
# save
np.savetxt("Score.csv", scores, delimiter=",")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.