我有一個簡單的 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
提交
取消
