mercredi 5 avril 2017

Writing the iterative output into a file in python

I am trying to write the output of my print statements into an output file instead of printing them at console. Is there any simple way to do that without affecting the code written in my print statements?

Code :-

outputfile = open('output1.txt','w')    
outputfile.write("Order_id  Order_date  User_id    Avg_Item_Price    Start_page_url     Error_msg")


for i in inputm[1:]:    
     if '::' in i[0] or ':' not in i[0]:        
         print('\n',"This is not a valid order record.") 
     else: 
         print('\n',i[0].split(':')[0]
                              ,str(datetime.strptime(i[0].split(':')[1],'%Y%m%d'))[:10]
                              ,i[1]
                              ,round(sum( float(v) if v else 0.0 for v in i[2:6])/4,2)
                              ,i[6] if Counter(i[6][0:23])  == Counter("http://www.google.com") else '                              '
                              ,'Valid URL' if Counter(i[6][0:23])  == Counter("http://www.google.com")  else 'Invalid URL'                
                          )

outputfile.close()  

Aucun commentaire:

Enregistrer un commentaire