I have a little python script which basically searches lines of a text file then returns a stripped version of the line back (with the numeric value I am looking for).
To do this, I get a user input (ui) which becomes the search I search each line of the text file for.
Problem I am having is that I can't seem to find a way to prevent it returning hits that is not a full word. For example, if the user searches "apple" I do not want it to return a line containing "applesauce".
I believe that one solution would be to convert the entire line to a list of individual words, then search the list for an exact hit. Would this be the best approach to take or is there a simple argument I can use somewhere which would be easier?
I'm sorry, my code is probably a mess to everyone else's eyes. I'm just a beginner with some basic VBA experience and now trying out python which seems to run MUCH faster for these tasks.
Thanks in advance!
#Ask for user input for variable name
print("Type variable name to be found:")
ui = raw_input()
#use userinput as name of file to be written
write_file = ("C:\\temp\\" + ui + ".csv")
for i in cmd_line_args:
with open(i) as dump:
lines = dump.readlines()
for line in lines:
if ui.lower() in line.lower():
line = line.replace(ui,"")
line = line.replace("=","")
b = ("abcdefghijklmnopqrstuvwxyz()?!£$:;@##_")
for char in b:
line = line.replace(char,"")
line = line.replace(" ","")
with open(write_file, "a") as f:
f.write(line)
print(line)
print("Operation complete, check " + write_file)
os.system('pause')
Heavily simplified sample data as requested:
Tested 18/01/10
USER mafs1f
ted = 1.040864 Description
frm2 = 1.082459 Description
orm = 0.4688 % Description
orm2 = -0.0469 % Description
AFS = 15.000 kg/h Description
msjfg = 7.500 kg/h Description
msdg = 7.500 kg/h Description
EnvJ = 978.00 hPa Description
Engfh = 1.9 degC Description
pact = 499.600 kPa Description