avisrivastava254084

Untitled

Oct 27th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import csv
  2. import re
  3.  
  4.  
  5. def read_file(input, output):
  6.     out = open(output, 'w')
  7.     csv_writer = csv.writer(out)
  8.     row = set()
  9.     with open(input, 'r') as file:
  10.         csv_reader = csv.reader(file, delimiter=',')
  11.         count, index = 0, 0
  12.         for line in csv_reader:
  13.             if count == 0:
  14.                 for i in range(len(line)):
  15.                     if line[i] == "LogbookIdent":
  16.                         index = i
  17.                         print("index", index)
  18.                         break
  19.                 count += 1
  20.                 continue
  21.             line[index] = re.sub("[^a-zA-Z0-9]", "", line[index])
  22.             line[index] = line[index].replace(' ', '')
  23.             if (line[index] in row):
  24.                 continue
  25.             row.add(line[index])
  26.             csv_writer.writerow(line)
  27.             count += 1
  28.  
  29.  
  30. read_file("/Users/aviralsrivastava/ShipLogbookID.csv",
  31.           "/Users/aviralsrivastava/ShipLogbookID_out.csv")
  32. read_file("/Users/aviralsrivastava/CLIWOC15.csv",
  33.           "/Users/aviralsrivastava/CLIWOC15_out.csv")
  34.  
  35.  
  36. '''
  37. Having a key value traversal in here. I could have used dictionary but wanted to
  38. grasp sets() as they are also used in a lot of DB implementation in Python3.
  39. The cleaned data is then placed into SQL and then used for querying.
  40.  
  41. The table o/p is here: https://pastebin.com/qxFjmbq1
  42. '''
Add Comment
Please, Sign In to add comment