python - How to add data(dict) to a specific fieldname(key) on a csv file? -
edit: sorry confusion i'll explain program for. it's keep track of users new weight record. file update when have exceeded previous weight record timestamp. want user able see time line of progress each lift using timestamp. why using lift['key']={data:dict} can reference each lift type , query date example lift['snatch']['5/25'] tell them maxed day. can't seem able write csv file properly. thank you time! happy friday!
i've been researching days , can't seem figure out how add data specific fieldname highest level key in dict.
the data want add dict in it's own.
how vision in csv file:
snatch <> squat <> jerk 10/25:150lbs <> 10/25:200lbs <> 10/25:0lbs
so how when created file. how able update 1 field.
say user squatted day , wants append data field.
what have far:
import time import csv lifts={} csv_columns = ['snatch','squat','jerk'] creation = time.strftime('%m:%s', time.localtime()) lifts['snatch']={creation:'150lbs'} lifts['squat']={creation:'200lbs'} lifts['jerk']={creation:'0lbs'} try: open(csv_file, 'w') csvfile: writer = csvdictwriter(csvfile, fieldnames=csv_columns) writer.writeheader() data in lifts: writer.writerow(lifts) except ioerror (errno, sterror): print("error") return
->one of issues when writers csv file writes on 3 times. not quite sure why. it's format want it's there 3 times.
-> want implement next code , write specific column, when writes null or blanks in other columns.
lifts['jerk'].update({time.strftime('%m:%s', time.localtime() : '160lbs'})
then out putting
snatch <> squat <> jerk 10/25:150lbs <> 10/25:200lbs <> 10/25:0lbs 10/26:160lbs
sorry i'm new python , not quit sure how use editor want result land under {10/25:0lbs}
show in excel.
it's important keep track of what's going on here: lifts
dictionary strings keys ("snatch", "squat", "jerk") , values dictionaries. second level of dictionaries has timestamp strings keys , strings values.
i suspect when want update lifts['jerk']
dictionary, don't use same key (timestamp) existing entry.
it doesn't seem need dictionary second level; consider using list instead, if must, can access so: lifts['jerk'][lifts['jerk'].keys()[0]]
rather hamfisted - again, consider either using different data type values of lifts
dictionary or use keys easier reference timestamps.
edit: lifts['jerk'] = {'timestamp':creation,'weight':'165lbs'}
requires restructuring of data.
Comments
Post a Comment