我有一个简单的 CSV 数据文件,它有两行,即Object_Id和 的VALUE每个Object ID索引在另一行 ( VALUE)中具有相同索引的对应值。我的目的是读取这些索引并使用预期数据验证这些数据。我能够读取 csv 文件,但不确定如何验证数据。这是 csv 文件的一部分:Obj ID, Value, Time Stamp13, 41.0, 2018-09-10 23:05:3014, 14.0, 2018-09-10 23:05:2013, 41.0, 2018-09-10 23:05:2014, 14.0, 2018-09-10 23:05:09这是我正在尝试的代码:import csvwith open('testoutfile1.csv', 'r') as csvfile:reader = csv.reader (csvfile, delimiter=';', quotechar='|')observed_output=[]expected_output=[]for row in reader: #print('; '.join(row)) observed_output = {row[0]:row[1]} print(observed_output)expected_output= {'Obj ID': 'Value','13':'41.0', '14':'14.0'}print(expected_output)for key in expected_output: if key in observed_output: print (key) print (observed_output[key]) print (expected_output [key]) if (observed_output[key])== (expected_output [key]): print ("Test Passed") elif (observed_output[key])!= (expected_output [key]): print ("Test Failed")这是我收到的输出,肯定缺少与其他条目/条目匹配的输出。你能评论吗?{'Obj ID': 'Value'}{'13': '41.0'}{'14': '14.0'}{'13': '41.0'}{'14': '14.0'}{'Obj ID': 'Value', '13': '41.0', '14': '14.0'}1414.014.0Test Passed
2 回答

素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
将expected_output在我看来应该是一个简单的字典
expected_output = {'13':'41.0', '14':'14.0'}
接下来你可以像这样继续
data = open('...')
next(data) # skip headers
for line in data:
id, val, *_ = [item.strip() for item in line.split(',')]
if id in expected_output and val == expected_output[id]:
# the observed output is the same as expected
...
else:
# observed is unexpected
...
添加回答
举报
0/150
提交
取消