[Tutor] Program gets stuck after a creating a list from dictinary items!

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Jul 6 18:22:31 CEST 2012


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

--



More information about the Tutor mailing list