MICListCC.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pandas as pd
  2. import json
  3. import numpy as np
  4. import requests
  5. import boto3
  6. def lambda_handler(event, context):
  7. try:
  8. excelURL = 'https://www.iso20022.org/sites/default/files/ISO10383_MIC/ISO10383_MIC.xls'
  9. imageRequest = requests.get(excelURL) # create HTTP response object
  10. fileName = 'ISO10383_MIC_Test.xls' # File Name to be saved as
  11. with open('/tmp/' + fileName, 'wb') as f:
  12. f.write(imageRequest.content)
  13. # Read 'MICs List by CC' sheet using Pandas library and create Dataframe
  14. # Store in /tmp folder
  15. fileLoad = pd.ExcelFile('/tmp/' + fileName)
  16. dfMIClistCC = fileLoad.parse('MICs List by CC')
  17. # Replace Nan from Dataframe and convert to empty string
  18. dfMIClistCC = dfMIClistCC.replace(np.nan, '')
  19. # Convert Panda Dataframe to Dictionary
  20. dfMIClistCC_dict = dfMIClistCC.to_dict('records')
  21. # Convert Dictionary to JSON
  22. dfMIClistCC_str = json.dumps(dfMIClistCC_dict)
  23. dfMIClistCC_json = json.loads(dfMIClistCC_str)
  24. # Save JSON file
  25. # Store in /tmp folder
  26. with open('/tmp/dfMIClistCC.json', 'w') as f:
  27. json.dump(dfMIClistCC_json, f)
  28. # Upload JSON file to S3 bucket
  29. data = open('/tmp/dfMIClistCC.json', 'rb')
  30. s3 = boto3.client('s3')
  31. s3.put_object(Bucket='bucket-miclistcc',
  32. Key='MICListCC.json',
  33. Body=data)
  34. return 'File uploaded successfully'
  35. except ConnectionError:
  36. print('Content does not exist')
  37. except FileNotFoundError:
  38. print('File not found')
  39. except Exception as e:
  40. print('Error occured: ' + str(e))