csv
文件正常工作。 dictionary
,但我似乎無法檢查csv
文件中的值,以確保我不添加重複條目。我怎樣才能檢查這個?我試過的代碼如下:Python詞典和CSV值|檢查CSV
def write_csv():
csvfile = csv.writer(open("address.csv", "a"))
check = csv.reader(open("address.csv"))
for item in address2:
csvfile.writerow([address2[items]['address']['value'],address2[items]['address']['count'],items, datetime.datetime.now()])
def check_csv():
check = csv.reader(open("address.csv"))
csvfile = csv.writer(open("address.csv", "a"))
for stuff in address2:
address = address2[str(stuff)]['address']['value']
for sub in check:
if sub[0] == address:
print "equals"
try:
address2[stuff]['delete'] = True
except:
address2[stuff]['delete'] = True
else:
csvfile.writerow([address2[stuff]['address']['value'], address2[stuff]['address']['count'], stuff, datetime.datetime.now()])
任何想法?
它以什麼方式不起作用?你有錯誤嗎?如果是這樣,有什麼錯誤? – BrenBarn 2013-04-09 03:45:50
你究竟想要做什麼? check_csv中的address2是什麼?有幾件事情你想改變,但是當你澄清你的問題時我們會解決這個問題。 – 2013-04-09 03:47:15
@Keeyai,我試圖確保'dictionary'' address2'中的新項目不在'csv'文件'address.csv'中。如果它已經在'csv'文件中,我想標記它被刪除(但不是真的刪除它),如果它不在'csv'中,則添加新行。它從上面引發'鍵錯誤',我無法弄清楚爲什麼(每個'key'項目單獨測試時,它工作正常) – jmitchel3 2013-04-09 05:50:42