Skip to content Skip to sidebar Skip to footer

Finding A String In Python And Writing It In Another File

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 e

Solution 1:

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...

forMember_Function_Keywordin 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:ifre.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.

Post a Comment for "Finding A String In Python And Writing It In Another File"