0

I am searching a way to check if there is a blank space next to the found keywords if so break the loop till it again finds the keyword.

For example here, I have a keyword:

'/* Function Declarations */'

If this keyword is found the loop will proceed further and check for the next set of keywords:

['(const ', '(unsigned ', '(char ', '(double ', '(int ', '(long ', '(long long ', '(float ', '(string ', '(signed ', ');']

If this is also satisfied the line will be sent to another function for processing. Until there is an empty space. This is what I wanted to achieve in this function. But unfortunately it ignores the empty space constrain and it satisfies only the first two

I have herewith attached the function:

def find_member_functions(opened_file, opened_cpp_file):

    previous_keyword = '/* Function Declarations */'

    member_func_keyword = ['(const ', '(unsigned ', '(char ',
                           '(double ', '(int ', '(long ',
                           '(long long ', '(float ', '(string ',
                           '(signed ', ');']

    for line in opened_cpp_file:
         prev_keyword_matched = [True for match in prev_keyword if match in line]
         if True in prev_keyword_matched:
             member_func_keyword_matched = [True for match in member_func_keyword if match in line]
             if True in member_func_keyword_matched: 
                 if line == '\n':
                     print "End of function declaration"
                     break
                 else:
                     found_funcs = member_functions(opened_file, line)
                     print str(found_funcs)
    return 

I then edited my code yet, I am not able to achieve what I wanted to. In this code, it says an empty line "End of member functions" even though it is not the end. The line which I am executing is

 /* Function Declarations */
  extern void multiplyImage(const unsigned char img[2115216], double parameter, 
  unsigned char imgout[2115216]);

  blabla.....

The functions finds the next line as a space though I have the continuation of the function

 unsigned char imgout[2115216]);

My edited code:

def Find_Member_Functions(Opened_File, Opened_Cpp_File):
    Previous_Line_Keyword = '/* Function Declarations */'
    Member_Function_Keyword = ['(const ', '(unsigned ', '(char ', '(double ', '(int ', '(long ', '(long long ', '(float ', '(string ', '(signed ', ');']
    Empty_Line = ['\n', '\r\n']
    for item in Member_Function_Keyword:
        for line in Opened_Cpp_File:
            Previous_Line_Keyword_Matched = [True for match in Previous_Line_Keyword if match in line]
            if True in Previous_Line_Keyword_Matched:
                Member_Function_Keyword_Matched = [True for match in Member_Function_Keyword if match in line]
                if True in Member_Function_Keyword_Matched:  
                    Found_Functions = Member_Functions(Opened_File, line)
                    print str(Found_Functions)
                    Next_Line = line[1:]
                    Empty_Line_Matched = [True for match in Empty_Line if match in Next_Line]
                    if True in Empty_Line_Matched:
                        print "End of member functions"
                        break
                    else:
                        pass
    return 

I couldn't understand where I am going wrong. It would be great to have some guidance...

My command line output

extern void multiplyImage(const unsigned char img[2115216], double parameter,

End of member functions

unsigned char imgout[2115216]);

End of member functions

user3652437
  • 201
  • 2
  • 8
  • 1
    How can the line both have keywords in it and be empty? And [again](http://stackoverflow.com/questions/23766362/finding-a-string-in-python-and-writing-it-in-another-file), please apply [PEP-8](http://legacy.python.org/dev/peps/pep-0008/) conventions to your code. – jonrsharpe May 21 '14 at 13:46
  • Hi, I wanted to check the next line if both constraints are satisfied and if there is a space in the next line then break the execution – user3652437 May 21 '14 at 14:36
  • Well that isn't what your code is doing - you are applying all tests to the same line. – jonrsharpe May 21 '14 at 14:39
  • But using line += line and then checking if the line has space also does't work... Is there any other way to find it ? – user3652437 May 21 '14 at 14:57
  • `line += line` just gives you the same line twice. You need to actually check the next/previous line, see e.g. [this question](http://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python). – jonrsharpe May 21 '14 at 15:00
  • Hi thanks it works fine, but it doesnt exactly do what I expect, it says a empty line found when it goes to the next line. When there is no empty line by the way ... – user3652437 May 21 '14 at 15:26
  • Please edit the question to include a minimal example of the code you're running and the input data it's taking along with the expected and actual outputs. – jonrsharpe May 21 '14 at 15:28
  • @jonrsharpe I have edited the question bit more, as you can see the command line output it says end of member functions after each function. But its not the case, as there is no empty space between any function. But I dont understand why it detects as empty space and prints End of member functions... – user3652437 May 22 '14 at 08:45

1 Answers1

0

This part of your code:

Next_Line = line[1:]
Empty_Line_Matched = [True for match in Empty_Line if match in Next_Line]

Makes absolutely no sense, for three principal reasons:

  1. line[1:] isn't the next line, it's just same line less its first character;
  2. For blank lines line == '\n', the first character is '\n', so line[1:] == "" and therefore Next_Line doesn't contain a newline character; and
  3. '\n' is at the end of every line, so Empty_Line_Matched will contain True in every case except a blank line (see #2).
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Ah !! Its the problem of the conceptualization of me... How to exactly go to the next line of a text file while reading ? I couldnt find a proper answer anywhere... As well as how to check if the whole line is empty then print the statement?.. – user3652437 May 22 '14 at 09:08
  • @user3652437 I have already provided a link to [this question](http://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python) that tells you how to iterate with `current` and `next_`. You can check if a line is empty with e.g. `if not line.strip():`. – jonrsharpe May 22 '14 at 09:12