Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import sys
  2.  
  3. import csv
  4.  
  5. import os
  6.  
  7. #f = str(sys.argv[1])
  8.  
  9. f = str(sys.argv[1])    #sys.argv[1] is the file that is dropped onto the script
  10.  
  11. g = str(os.getcwd() + r'\RECURSIVEHIERACHY.csv') #output is the current working directory + filename <-
  12.  
  13. #print g if you can't find the file!!!
  14.  
  15. csvdata = csv.reader(open(f))
  16.  
  17. def recursesearch(line):
  18.  
  19.     try:
  20.  
  21.         if line[-3] in dic.keys():
  22.  
  23.             line.extend(dic[line[-3]][-3:])
  24.  
  25.             recursesearch(line)
  26.  
  27.     except IndexError:
  28.  
  29.         return
  30.  
  31. block = []
  32.  
  33. for item in csvdata:
  34.  
  35.     block.append(item)
  36.  
  37. dic = {}
  38.  
  39. for line in block:
  40.  
  41.     dic[line[0]] = line[:]
  42.  
  43. for line in block:
  44.  
  45.     recursesearch(line)
  46.  
  47. output = csv.writer(open(g, 'wb'))
  48.  
  49. for line in block:
  50.  
  51.     output.writerow(line)
Add Comment
Please, Sign In to add comment