Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import pandas as pd
- #s = "<description>Arahura - Dobson A</description>"
- #result = re.search('<description>(.*)</description>', s)
- #searcher = result.group(1)
- #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.
- #it requires heavily on patterns, and requires a VARIABLE to take the new word.
- replace_word = "OPVOLT_VAR" #word to replace, iterate over a few times, replacing each VAR one at a time
- #filepaths etc
- filepath = 'Transmission Lines.txt'
- excel_pf = 'Transmission_Lines.csv'
- #### VARS ###
- NL = '\n'
- with open(filepath, 'r') as HP: #open file and store as list
- text = HP.readlines() #read entire text into list
- #### PANDA STUFF #####
- data = pd.read_csv(excel_pf) #read excel
- data = pd.DataFrame(data) #convert to DF
- #find something in DF
- #df = pd.DataFrame(data, columns = [name_test])
- #found_data = data[df] # create new object with just what Im looking for
- #print(found_data.head()) #for debugging
- #opvolt = found_data.iloc[0, 2] #search through data string for OPVOLT rating
- #print("The OPVOLT IS: %.0i" %opvolt)
- for i, line in enumerate(text):#iterate through list
- #print("Line {}: {}".format(i, line.strip())) #for debugging
- 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
- #if i == 816:
- # print("HERE BY BUGS")
- if result: #if found
- REPLACER = result.group(1) #what im looking for
- #if REPLACER == "Otahuhu - Penrose B": #debugging
- # print("HERE BE BUGS") #debugging
- replace_line = text[i+2] #line that variable is on. In this case it is OPVOLT and 1 line after the name
- data_index = data["description"] #indexing, find what im looking for
- #if i == 14963: #debugging
- # print("STOP") #debugging
- 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
- found_data = data[df] #cut panda into a 1xN array containing just what I want
- #print(found_data.head()) #for debugging
- 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.
- #opvolt = opvolt + "\n" #adding in line break for readability in txt file
- #print("The OPVOLT IS FOR %s: %.0i" %(REPLACER, opvolt) #for debugging
- #print("The OPVOLT IS FOR %s is: " %(REPLACER)) #for debugging
- #print(opvolt) #for debugging
- #print("FOUND SOMETHING - REPLACE HERE: %s" %(replace_line)) #for debugging
- 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
- #replace_line = text[i-1] # for debugging
- #print("REPLACED TEXT IS: %s" %(replace_line)) #for debugging/testing
- #print("RUN THROUGH THIS CODE")
- #save file as new
- save_file = "new_" + filepath
- with open(save_file, 'w') as f:
- for item in text:
- f.write("%s" % item)
- print("CODE COMPLETED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement