Advertisement
Mark2020H

Solution for https://www.facebook.com/pallab.nandi.395

Jul 31st, 2022 (edited)
1,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. ''' Shebang  so we can make this exe on linux using  chmod + x <filename.py> '''
  4.  
  5.  
  6. # importing pandas
  7.  
  8. '''https://datatofish.com/import-csv-file-python-using-pandas/'''
  9.  
  10. import pandas as pd
  11. import math
  12. import shutil
  13. import os
  14. import glob
  15.  
  16.  
  17.  
  18. m_filename = 'mytext.txt'
  19. m_newFile = 'output.txt'
  20.  
  21. def readmytextfile(f):
  22.     # read text file into pandas DataFrame
  23.     df = pd.read_csv(f, sep=" " ,header=None  ,names=["Value1", "Value2"])
  24.     # display DataFrame
  25.     return df
  26.  
  27. def calculateXY(m_df):
  28.    
  29.     ''' https://www.w3schools.com/python/python_file_remove.asp '''
  30.    
  31.     # delete last files written
  32.     if os.path.exists("output.txt"):
  33.         os.remove("output.txt")
  34.    
  35.     # create a header for  column names
  36.     names = ["Sin Value 1", "Cos Value 2", "Sum Val 1 & Val 2"]
  37.    
  38.    
  39.     # create  text file to store results
  40.     with open('output.txt', 'w') as f:
  41.         f.write("\t".join(names))
  42.         f.close()
  43.        
  44.    
  45.     # get the number of rows
  46.    
  47.     numRows = m_df.shape[0]
  48.     # get the umber of cols
  49.     numCols = m_df.shape[1]
  50.    
  51.    
  52.    
  53.     #print these results to standard io
  54.    
  55.     for y in range(3):
  56.         print (" ")
  57.    
  58.     print("Number of rows = " + str(numRows))
  59.     print("Number of cols = " + str(numCols))
  60.    
  61.    
  62.     # iterate through rows and perform  calculations
  63.    
  64.     ''' https://docs.python.org/3/library/math.html'''
  65.    
  66.    
  67.    
  68.     for x in range(numRows):
  69.         # get the sin of 1st value
  70.         a= math.sin(m_df.iat[x,0])
  71.         ''' convert to 3 decimal places '''
  72.         a = float("{:.3f}".format(a))      
  73.         # get the cos of second  value
  74.         b= math.cos(m_df.iat[x,1])
  75.         ''' convert to 3 decimal places '''
  76.         b = float("{:.3f}".format(b))              
  77.         # store the result of addition in  c
  78.         c= a + b
  79.         # print these results  for  debug
  80.         # print (str(a) + '\t'+str(b) + '\t'+ str(c))
  81.        
  82.         # write the results to the file
  83.         with open('output.txt', 'a+') as f:
  84.             f.seek(0)
  85.             data = f.read(1000)
  86.            
  87.             if len(data) > 0:
  88.                
  89.                 f.write("\n")
  90.                 f.write(str(a) + '\t'+str(b) + '\t'+ str(c))
  91.                 f.close()
  92.                        
  93.        
  94.  
  95. def readNewfile(f):
  96.     # read text file into pandas DataFrame
  97.     dn = pd.read_csv(f, sep="\t")
  98.     # display DataFrame
  99.     return dn      
  100.        
  101.  
  102.  
  103. ''' this will be the main method '''
  104. def main():
  105.    
  106.     # clear console screen
  107.     os.system('clear')
  108.  
  109.     m_dataframe = readmytextfile(m_filename)
  110.     print(" ")
  111.     print("Original file  values")
  112.    
  113.     for  j in range(3):
  114.         print(" ")
  115.        
  116.     print(m_dataframe)
  117.     calculateXY(m_dataframe)
  118.     n_dataframe = readNewfile(m_newFile)
  119.     for  j in range(3):
  120.         print(" ")
  121.    
  122.     print("Results calculated from myfile.txt")
  123.     print(" " )
  124.    
  125.     print(n_dataframe)
  126.     print ("Creating csv file named output.csv")
  127.    
  128.     '''https://docs.python.org/3/library/shutil.html'''
  129.    
  130.     shutil.copyfile('output.txt', 'output.csv')
  131.     print("Showing files in directory " )
  132.    
  133.     '''https://datagy.io/list-files-os-glob/'''
  134.    
  135.     print(glob.glob("*.*"))
  136.    
  137.    
  138.    
  139.  
  140. if __name__ == '__main__':
  141.    main()
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement