Advertisement
krishnaetl2102

python code automation

Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. hi team i was trying to do read all the files from the directory instead of this am reading only one file at directory some one please suggest me the code how to read all files from the directory (like automation) some please suggest me the code
  2. below is my script is it suitable for automation
  3. import os, sys, csv,json
  4. from itertools import product
  5. import requestsfrom requests_aws4auth
  6. import AWS4Authimport boto3import botocore
  7. import configfrom elasticsearch
  8. import helpers,Elasticsearches = Elasticsearch("localhost:9200")
  9.  
  10. s3 = boto3.resource('s3')
  11. ACCESS=config.s3bucket['aws_access_key']
  12. SECRET=config.s3bucket['aws_secret_key']
  13. url_e=config.s3bucket['url']
  14.  
  15. client = boto3.client(service_name="s3", region_name="us-east-1",endpoint_url=url_e,verify=False,aws_access_key_id = ACCESS,aws_secret_access_key= SECRET)
  16.  
  17. client.download_file('s3_bucket','sourcefile.csv','sourcefile.csv')
  18. print ("Downloading object %s from bucket %s" % ('sourcefile.csv','s3_bucket'))
  19.  
  20. #step1: file split into two files
  21.  
  22. if os.path.exists('file1.csv'):
  23.     print('File is previously present and have been deleted !')
  24.     os.remove('file1.csv')
  25. if os.path.exists('file2.csv'):
  26.     print('File is previously present and have been deleted !')
  27.     os.remove('file2.csv')
  28.  
  29. condition = 0
  30. header = []
  31.  
  32. with open('sourcefile.csv', 'r', encoding='UTF-8') as file:
  33.     for lines in file:
  34.         if condition ==0:
  35.             header = lines.strip('\n').split(',')
  36.         elif condition < 2:
  37.             with open('file1.csv', 'a', encoding='UTF-8') as file_01:
  38.                 if os.stat('file1.csv').st_size == 0:
  39.                     file_01.write(','.join(header) + '\n')
  40.                     file_01.write(lines.strip('\n') + '\n')
  41.         elif condition >= 3:
  42.             with open('file2.csv', 'a', encoding='UTF-8') as file_02:
  43.                 file_02.write(lines.strip('\n') + '\n')
  44. condition += 1
  45. print('file spliting completed')
  46.  
  47. #step2:merging both files and updating header.
  48.  
  49. with open(r'file1.csv','rt')as f:
  50.     data1 = csv.reader(f,delimiter=",")
  51.     list1= [row for row in data1]
  52. with open(r'file2.csv','rt')as f:
  53.     data2 = csv.reader(f,delimiter=",")
  54.     list2= [row for row in data2]
  55. header= ["dept_id","dept_role","dept_name","dept_date","dept_service","id","name","start_time","end_time","user","manufacturing","exp"]
  56. if os.path.exists('BAY23_merge.csv'):
  57.     print('File is previously present and have been deleted !')
  58.     os.remove('BAY23_merge.csv')
  59. with open('file_merge.csv', 'w', newline='') as csvfile:
  60.     csvwriter = csv.writer(csvfile)
  61.     csvfile.write(','.join(header)+'\n')
  62.     for r in list2[1:]:
  63.         csvwriter.writerow(list1[1]+r)
  64. print('files has merged and header has updated')
  65.  
  66. #step3:loading data into elasticsearch:
  67.  
  68. from elasticsearch import helpers,Elasticsearches = Elasticsearch("localhost:9200")
  69. with open('file_merge.csv') as f:
  70.     reader = csv.DictReader(f)
  71.     helpers.bulk(es, reader, index='test_data_sample', doc_type='test-sample')
  72.    
  73. print('data loaded into ES')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement