利用正則表達式,去除python文件中所有註釋

簡介
有時候代碼的註釋寫的太多,反而影響對程序的理解

手動刪除所有註釋代碼,又過於麻煩

所以基於 文件讀寫,異常處理,正則字符串 等知識

寫了這麼個小程序

程序特點
在任意位置終端(cmd)中運行此程序,將需要刪除註釋的.py文件,拖拽到終端窗口中。都可以在源文件路徑下,生成無註釋版本的文件.py 和 註釋的備份 .txt文件

源代碼

import re
 
 
def file_analysis(old_file_lines, six_quotes, hashtap):
    """標記需要刪除的註釋的行號,並存入列表"""
    i = 0
    for line in old_file_lines:
        # 符號 # 獨佔一行
        ret_1 = re.match(r"^[^\w]*#+",line)
        if ret_1:
            hashtap.append(i)
        # 符號 """ 獨佔一行
        ret_2 = re.match(r"[ ]*\"\"\"",line)
        if ret_2:
            # 如果存在類型,函數說明的 """xxxxx""" 之類的,不予刪除
            ret_2_1 = re.match(r"[^\"]*\"\"\"[^\"]*\"\"\"",line)
            if ret_2_1:
                pass
            else:
                six_quotes.append(i)
        i += 1
    # 將兩個"""行號之間所有的行添加到 # 號列表中
    while six_quotes != []:
        # 從列表中移出最後兩個元素
        a = six_quotes.pop()
        b = six_quotes.pop()
        temp = b
        while temp <= a:
            hashtap.append(temp)
            temp += 1
    # 返回 # 號列表, 記返回需要刪除的所有註釋的 行號 集合
    return hashtap
 
 
def main():
    """ 主函數"""
    # 1,獲取路徑,並讀取此文件
    # 1.1 獲取文件名及其路徑
    print("\r\n"*3)
    file_name = input("請輸入需要刪除註釋的目標文件(形如:file.py):")
    # 1.2 讀取文件
    try:
        f = open(file_name, "rb")
        old_file = f.read()
        f.close()
    except:
        print("無法打開文件:" + file_name)
    else:
        # 2,處理文件
        # 2.1 讀取文件成功,文件解碼並按行切割成列表
        old_file = old_file.decode("utf-8")
        old_file_lines = old_file.splitlines()
        # 2.2 處理文件並得到需要刪除的註釋的行號集合
        six_quotes, hashtap = list(), list()
        hashtap = file_analysis(old_file_lines, six_quotes, hashtap)
        # 此時返回值 hashtap列表中,不僅僅包含#,還有"""的行號
        try:
            # 3,獲取 註釋和無註釋 內容到列表中
            # 3.2 去重並排序,得到所有註釋行號的列表
            comment_list = sorted(set(hashtap))
            # 3.3 創建存儲(備份)註釋文件內容的列表
            comment_file = list()    
            for i in comment_list:
                comment = old_file_lines[i]
                comment_file.append(comment)
            # 創建與源文件總行號相同的列表 0,1,2,3...
            new_file_list = list(i for i in range(len(old_file_lines)))
            # 刪除註釋的行號,留下無註釋的行號 的列表集合
            for i in comment_list:
                new_file_list.remove(i)
            # 3.4 創建存儲(無註釋)新文件內容的列表
            new_file_lines = list()
            for i in new_file_list:
                temp = old_file_lines[i]
                new_file_lines.append(temp)
            
        except:
            print("待處理代碼中沒有註釋")
        else:
            # 4,在文件路徑新建兩個文件,並寫入數據到文件
            ret = re.match(r"([^ ]+).py",file_name)
            if ret:
                file_name_pre = ret.group(1)
                # 5,分別新建 “乾淨版”文件,和“註釋集合”文件
                with open(file_name_pre + "(無註釋版).py","wb") as f:
                    for i in new_file_lines:
                        f.write(i.encode("utf-8"))
                        f.write("\r\n".encode("utf-8"))
                with open(file_name_pre + "(註釋存檔).txt", "wb") as f:
                    for i in comment_file:
                        f.write(i.encode("utf-8"))
                        f.write("\r\n".encode("utf-8"))
                print("\r\n"*3)
                print("--------創建成功!!!--------")
                print("\r\n"*3)
            else:
                print("正則字符串,無法識別文件的路徑")
 
            
if __name__ == "__main__":
    main()


Demo示例

print("程序中常見的註釋")
 
注意:此程序將會刪除的,會在Demo註釋末尾添加 YES,不會刪除的 NO
 
# 這是第一種註釋,'#'放在開頭(YES)
 
ret = analysix(data)  # 這是第二種註釋,'#'跟在某一個語句後面(NO)
 
"""
第三種註釋
有時候需要註釋掉某一整塊東西的時候,使用這個
(YES)
"""
 
""" 第四種註釋,這是函數或者類的說明(NO)"""
 
    # 這是第四種註釋,'#'前面加了空格(YES)


 


生成的無註釋文件

print("程序中常見的註釋")
 
注意:此程序將會刪除的,會在Demo註釋末尾添加 YES,不會刪除的 NO
 
 
ret = analysix(data)  # 這是第二種註釋,'#'跟在某一個語句後面(NO)
 
 
""" 第四種註釋,這是函數或者類的說明(NO)"""


 


生成的註釋備份文件


# 這是第一種註釋,'#'放在開頭(YES)
"""
第三種註釋
有時候需要註釋掉某一整塊東西的時候,使用這個
(YES)
"""
    # 這是第四種註釋,'#'前面加了空格(YES)


 
————————————————
版權聲明:本文爲CSDN博主「programminghkl」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/programminghkl/article/details/97433707

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章