Advertisement
Guest User

DAVE_TXT_STUFF

a guest
Dec 10th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. import re
  2. import pandas as pd
  3.  
  4.  
  5. #s = "<description>Arahura - Dobson A</description>"
  6. #result = re.search('<description>(.*)</description>', s)
  7. #searcher = result.group(1)
  8.  
  9. #this program searches through a text document to find a word between two other words, and then inserts that into a VARIABLE in the document.
  10. #it requires heavily on patterns, and requires a VARIABLE to take the new word.
  11.  
  12. replace_word  = "OPVOLT_VAR" #word to replace, iterate over a few times, replacing each VAR one at a time
  13. #filepaths etc
  14. filepath = 'Transmission Lines.txt'
  15. excel_pf = 'Transmission_Lines.csv'
  16.  
  17. #### VARS ###
  18. NL = '\n'
  19.  
  20.  
  21. with open(filepath, 'r') as HP: #open file and store as list
  22.     text = HP.readlines() #read entire text into list
  23.  
  24.  
  25. #### PANDA STUFF #####
  26.  
  27. data = pd.read_csv(excel_pf) #read excel
  28. data = pd.DataFrame(data) #convert to DF
  29.  
  30. #find something in DF
  31.  
  32. #df = pd.DataFrame(data, columns = [name_test])
  33.  
  34. #found_data = data[df] # create new object with just what Im looking for
  35. #print(found_data.head()) #for debugging
  36. #opvolt = found_data.iloc[0, 2] #search through data string for OPVOLT rating
  37. #print("The OPVOLT IS: %.0i" %opvolt)
  38.  
  39.  
  40. for i, line in enumerate(text):#iterate through list
  41.     #print("Line {}: {}".format(i, line.strip())) #for debugging
  42.     result = re.search('<name>(.*)</name>', line) #look for word between these two words, in this case it is the name of the TX, but can change as long as it fits in the pattern
  43.     #if i == 816:
  44.     #    print("HERE BY BUGS")
  45.     if result: #if found
  46.             REPLACER = result.group(1) #what im looking for
  47.            
  48.             #if REPLACER == "Otahuhu - Penrose B": #debugging
  49.             #    print("HERE BE BUGS") #debugging
  50.  
  51.             replace_line = text[i+2] #line that variable is on. In this case it is OPVOLT and 1 line after the name
  52.  
  53.             data_index = data["description"] #indexing, find what im looking for  
  54.  
  55.             #if i == 14963:  #debugging
  56.             #    print("STOP") #debugging
  57.            
  58.  
  59.             df = data['description'] == REPLACER #find appropiate line in the panda, where "description" is the row im searching for, and REPLACER is the word. REPLACER = unique TX data
  60.             found_data = data[df] #cut panda into a 1xN array containing just what I want
  61.             #print(found_data.head()) #for debugging
  62.             opvolt = str(found_data.iloc[0, 2]) #search through data string for OPVOLT rating. Data in [] is the indexing. Need to manually change it as appropiate.
  63.             #opvolt = opvolt + "\n" #adding in line break for readability in txt file
  64.             #print("The OPVOLT IS FOR %s: %.0i" %(REPLACER, opvolt) #for debugging
  65.             #print("The OPVOLT IS FOR %s is: " %(REPLACER)) #for debugging
  66.             #print(opvolt) #for debugging
  67.  
  68.             #print("FOUND SOMETHING - REPLACE HERE: %s" %(replace_line)) #for debugging
  69.            
  70.             text[i+2] = text[i+2].replace(replace_word, opvolt, 1) #insert word into text at line indicated by +N. This is manually changed, depeding on the pattern
  71.             #replace_line = text[i-1] # for debugging
  72.             #print("REPLACED TEXT IS: %s" %(replace_line)) #for debugging/testing
  73.             #print("RUN THROUGH THIS CODE")
  74.            
  75.  
  76.  
  77. #save file as new
  78. save_file = "new_" + filepath
  79.  
  80. with open(save_file, 'w') as f:
  81.     for item in text:
  82.         f.write("%s" % item)
  83.  
  84.  
  85. print("CODE COMPLETED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement