Python Dictionaries & CSV Values | Check CSV -


the csv file works fine. dictionary can't seem check values in csv file make sure i'm not adding duplicate entries. how can check this? code tried below:

    def write_csv():         csvfile = csv.writer(open("address.csv", "a"))         check = csv.reader(open("address.csv"))         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"))         stuff in address2:             address = address2[str(stuff)]['address']['value']             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()]) 

any ideas?

your csv , dict structures little wonky - i'd love know if set or if can change them more useful. here example want -- you'll have change things fit format. important change not writing file reading - going lead headaches.

this asked delete flag -- there external need this? if not there better way (removing bad rows, saving rows somewhere else, etc - depends on doing).

anyway, here example. used commented block create csv file in first place, added new address list , ran rest. instead of looping through file on , on makes lookup dict address , stores row number, uses update delete flag if found when reads csv file. you'll want take prints out , uncomment last line write new rows.

import csv, datetime  addresses = [     {'address': {'value': '123 road', 'count': 1}, 'delete': false},     {'address': {'value': '456 road', 'count': 1}, 'delete': false},     {'address': {'value': '789 road', 'count': 1}, 'delete': false},      {'address': {'value': '1 new road', 'count': 1}, 'delete': false}, ]  = datetime.datetime.now()  ### create csv ##with open('address.csv', 'wb') csv_file: ##    writer = csv.writer(csv_file) ##    row in addresses: ##        writer.writerow([ row['address']['value'], row['address']['count'], now.strftime('%y-%m-%d %h:%m:%s') ])  # make lookup keys dict address_lookup = {} in range(len(addresses)):     address_row = addresses[i]     address_lookup[address_row['address']['value']] =  # read csv once open('address.csv', 'rb') csv_file:     reader = csv.reader(csv_file)     row in reader:         print row         # if address found in dict, set delete flag true         if row[0] in address_lookup:             print 'flagging address old: %s' % row[0]             addresses[ address_lookup[row[0]] ]['delete'] = true  open('address.csv', 'ab') csv_file:     # go through addresses , add shouldnt deleted csv     writer = csv.writer(csv_file)     address_row in addresses:         if address_row['delete'] false:             print 'adding row: '             print address_row             #writer.writerow([  row['address']['value'], row['address']['count'], now.strftime('%y-%m-%d %h:%m:%s') ]) 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -