问题描述
我正在寻找一种方法来检查找到的关键字旁边是否有空格,如果有的话,中断循环,直到它再次找到关键字.
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:
'/* 函数声明 */'
如果找到此关键字,循环将继续进行并检查下一组关键字:
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 ','(签名', ');']
如果这也满足,该行将被发送到另一个函数进行处理.直到有空位.这就是我想在这个函数中实现的.但不幸的是它忽略了空空间约束,只满足前两个
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
我附上了这个函数:
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]);
我编辑的代码:
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...
我的命令行输出
extern void multiplyImage(const unsigned char img[2115216], double参数,
成员函数结束
无符号字符输出[2115216]);
unsigned char imgout[2115216]);
成员函数结束
推荐答案
这部分代码:
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:
line[1:]
不是下一行,它只是同一行减去第一个字符;- 对于空行
line == '\n'
,第一个字符是'\n'
,所以line[1:] == ""
因此Next_Line
不 包含换行符;和 '\n'
位于每一行的末尾,所以Empty_Line_Matched
将包含True
case 除了一个空行(见#2).
line[1:]
isn't the next line, it's just same line less its first character;- For blank lines
line == '\n'
, the first character is'\n'
, soline[1:] == ""
and thereforeNext_Line
doesn't contain a newline character; and '\n'
is at the end of every line, soEmpty_Line_Matched
will containTrue
in every case except a blank line (see #2).
这篇关于检查关键字后是否有空格,如果在python中找到则中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!