Finding a string in python and writing it in another file [closed]
2024/11/16 11:24:39
I have written a function in Python to find a string and send the data in the line at which the string is found to another function to store the data. But some how I dont get any error nor it does what I would like it to do.
I have attached the code snippet
def Find_Member_Functions(Opened_File, Opened_Cpp_File):Member_Function_Keyword = '( 'Found_Functions = ''for line in Opened_Cpp_File:if re.match(Member_Function_Keyword, line):Found_Functions = Member_Functions(Opened_Cpp_File, line)Member_Functions = Member_Functions(Opened_File, Found_Functions)print str(Found_Functions)else:print "No keyword could be matched finding again \n"breakreturn Found_Functions
Answer
There are two things dodgy here that I think may be contributing to this.
Strange thing 1:
Strangely, when I even attempt to run this regex, I get an error and I can't see why you do not get this. When I attempt to run this:
line = 'hello'
Member_Function_Keyword = "( "
if re.match(Member_Function_Keyword, line):
I get: sre_constants.error: unbalanced parenthesis
This makes me presume that your program never gets to your regex. I imagine this is because of the way you strip your string - You end up with nothing in Iterating_Function so your regex is never called.
Strange Thing 2:
You use break OUTSIDE of the if statement. Your for loop will break on its first loop regardless of what happens. So, whatever the first line contains will always end the loop so nothing will be returned. However, I find this strange because you say nothing happens at all, not even the else statement is printed out. This again leads me to the conclusion that you must have one of the following problems:
Your regex is wrong. Not likely as no error is produced
Since no error is produced - regex is never called. This leads me to think either...
Your File is empty or something in the strange way you iterate over the file twice is emptying it.
I realise that this is not a solution to your problem however I hope it can help you to debug it. I would defiantly check the contents of your file, check that you are calling .read() on your file somewhere, do something about the strange iterations you are doing and finally the indentation on your break statements.
Update Possible Answer:
Re-looking at your code, I am noticing very strange thigs. Firstly, You Define Member_Function_Keyword, then preceed to overwite it by using it in your for loop...
for Member_Function_Keyword in Iterating_Function:
This means that, for each letter in iterating function, you are saving this letter as Member_Function_Keyword and then checking if it is the first letter of the current line which brings me to my next point...
You are removing the lines from your file with this line
Iterating_Function = [line.strip() for line in Opened_Cpp_File]
Then, iterating over the lines in the file with the next line. This seems odd.
So, with these strange things taken into account and that fact that you haven't really told us what you want this function to do, I am going to try and make a few presumptions. I am going to presume that you are looking for a bracket at the start of a line in the file. So, here is how you would do that:
for line in Opened_Cpp_File:if re.match(Member_Function_Keyword, line):#Do something here...#NOTE, you were doing this:#Found_Functions = Member_Functions(Opened_File, line)#This is 'recursion' and I can't see why you want itelse:#Do something else
Or, if you want to stop when you find a line that contains a bracket:
for line in Opened_Cpp_File:if re.match(Member_Function_Keyword, line):#Do something here...breakelse:#Do something else
Sadly, with the vagueness of the question, I cannot do much more. I hope this will help though.
Traceback (most recent call last):File "wdd.py", line 164, in <module>file.write("temperature is ", temperature, "wet is ", humidity, "%\n")
TypeError: fun…
Description:
In the django session docs it says:You can read it and write to request.session at any point in your view.But I cant access the session when making a second request to the same view:
views…
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…
So, Im trying to tackle the TSP with a Genetic Algorithm. To do that I need to create a population pool. What I want to accomplish is to create a list of random permutations that will represent a popul…
Im trying to extract data from a directory with 12 .txt files. Each file contains 3 columns of data (X,Y,Z) that i want to extract. I want to collect all the data in one df(InforDF), but so far i only …
If I have a string named link, how would I go about checking to see if it follows the same format as a wikipedia URL? To clarify, wikipedia URLs (in this case) always begin with en.wikipedia.org/wiki/…
Id like to create a dictionary using dictionary comprehension syntax.Note that list l contains tuples of strings and tuples with 1st element always a time stamp.This works:d = {}
for entry in l:if entr…
I want to find the next empty cell in a specific column and write to values that cell. Ive tried it using following method:
for row in sheet[A{}:A{}.format(sheet.min_row,sheet.max_row)]:if row is None:…
I am using Google map api and i am trying to pass the data (longitude and latitude) to the template then use the data in the javascript to show a specific location.location.html{% for venue in propert…