I could resolve it by defining a small function: def getValue(mydict,keys): A=[]; for i in keys: A=A+[mydict[i]] return A
and then calling it: D=getValue(prog,sCommonFeatures); (instead of D=[prog[key1] for key1 in list(sCommonFeatures)];) but I'm still surprised why the latter one didn't work! On Fri, Jul 6, 2012 at 12:28 PM, Ali Torkamani <torkam...@gmail.com> wrote: > > BTW I changed it to: > D=[prog[key1] for key1 in list(sCommonFeatures)] > > because sCommonFeatures is actually a set, but it still has the smae problem > > > On Fri, Jul 6, 2012 at 12:27 PM, Ali Torkamani <torkam...@gmail.com> wrote: >> >> Thanks, I checked, FD is not empty, >> Thanks for reminding about the flush and close, but I think some thing is wrong with: >> >> D=[prog[key1] for key1 in sCommonFeatures] >> >> In the debug mode it works fine from the command line, but in the for loop it gets stuck. >> >> Ali >> >> >> On Fri, Jul 6, 2012 at 12:22 PM, Prasad, Ramit <ramit.pra...@jpmorgan.com> wrote: >>> >>> From: Ali Torkamani >>> > I'm trying to write a dictionary into a csv file, in the following code >>> > FlatData is the global dictionary whose keys are datetime objects, and the >>> > values are list of dictionaries. sCommonFeatures are key's that exist in all >>> > of such 'sub'-dictionaries, and sOtherFeatures are the key's that are in some >>> > of sub-dictionaries. >>> > >>> > The problem is that in line : D=[prog[key1] for key1 in sCommonFeatures] >>> > (line 10 below), the program stucks! >>> >>> Gets stuck how? I see nothing in that line that looks invalid. It may just >>> take awhile depending on the amount of keys it has to go through. Maybe >>> you just need to wait longer? >>> >>> > >>> > I appreciate any help. >>> > >>> > def WriteOneDayToCSV(sCommonFeatures,sOtherFeatures,date): >>> > FD=FlatData[date] >>> > >>> > A=['UniqeDate','Year','Month','Day']+list(sCommonFeatures)+list(sOtherFeatures >>> > ) >>> > pdb.set_trace() >>> > with >>> > open(str(date.year)+"_"+str(date.month)+"_"+str(date.day)+".csv",'wb') as >>> > myfile: >>> > fout = csv.writer(myfile, delimiter=',', quotechar="'") >>> > >>> > fout.writerow(A) >>> > for prog in FD: >>> >>> Try inserting a print here to make sure FD is not an empty list. >>> >>> > D=[prog[key1] for key1 in sCommonFeatures] >>> > print(prog) >>> > pdb.set_trace() >>> > diff=sOtherFeatures.difference(prog.keys()) >>> > prog.update({x:'NONE' for x in diff}) >>> > >>> > C=[prog[key2] for key2 in sOtherFeatures] >>> > A=["%4d%02d%02d" >>> > %(date.year,date.month,date.day),date.year,date.month,date.day] >>> > fout.writerow(A+D+C) >>> > pdb.set_trace() >>> > myfile.flush() >>> > myfile.close() >>> >>> There is no need to flush/close a file that is opened using "with". >>> >>> Ramit >>> >>> >>> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology >>> 712 Main Street | Houston, TX 77002 >>> work phone: 713 - 216 - 5423 >>> >>> -- >>> >>> _______________________________________________ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >> >> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor