Python 如何计算java文件中的注释行?

Python 如何计算java文件中的注释行?,python,Python,这是我计算java文件中不同行数代码的一部分。在大多数情况下,此代码可以获得正确的编号。但是如果注释行如下所示: /* * */ 它将此块视为只有两条线 for eachLine in allLines: if eachLine != " " : eachLine = eachLine.replace(" ",""); #remove space eachLine = self.trim(ea

这是我计算java文件中不同行数代码的一部分。在大多数情况下,此代码可以获得正确的编号。但是如果注释行如下所示:

/*

*

*/

它将此块视为只有两条线

        for eachLine in allLines:
            if eachLine != " " :
                eachLine = eachLine.replace(" ",""); #remove space
                eachLine = self.trim(eachLine);      #remove tabIndent
                if  (iscomment==False):
                    if(eachLine.strip().startswith("//")): #LINECOMMENT 
                        commentCount += 1;
                    if eachLine == "":
                        blankCount += 1;
                    if(eachLine.strip().startswith("/*")):
                        commentCount += 1;
                        if(not eachLine.strip().endswith("*/")):
                            iscomment=True
                else :
                    commentCount += 1;
                    if(eachLine.find("*/")):
                        iscomment=False
            lineCount = lineCount + 1;
        codeCount=lineCount-commentCount-blankCount
失败时返回-1,不是false

你应该考虑:

在我的一个非常简单的项目中,仅此一行就尝试了一些东西,它似乎获得了适当数量的行。

看看
if(eachLine.find("*/")):
import re

comments = re.findall('#.*?\n', allLines)
for item in re.findall('/\*.?*\*/', allLines):
    for row in item.split('\n'):
        comments.append(row)
print(len(comments))