吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 567|回复: 8
收起左侧

[Python 原创] 去除包含中文字符的代码行

[复制链接]
Mis16800 发表于 2024-5-10 10:49
本帖最后由 Mis16800 于 2024-5-11 09:23 编辑

去除包含中文字符的代码行

[md]```
import os
import re

def remove_chinese_code_and_comments_in_folder(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file_name in files:
            if file_name.endswith((".java", ".js", ".py")):
                file_path = os.path.join(root, file_name)
                with open(file_path, "r", encoding="utf-8") as file:
                    code = file.read()

                    cleaned_code = remove_chinese_code_and_comments(code)

                    file.seek(0)
                    file.truncate()
                    file.write(cleaned_code)

def remove_chinese_code_and_comments(code):
    lines = code.split('\n')
    result = []
    in_comment_block = False

    for line in lines:
        # 去除包含中文字符的注释行
        if "//" in line or "#" in line or "/*" in line:
            if "//" in line:
                line_parts = line.split('//', 1)
            elif "#" in line:
                line_parts = line.split('#', 1)
            else:
                line_parts = re.split('/\*', line, 1)
            comment = line_parts[-1].strip()
            if re.search('[\u4e00-\u9fff]', comment):
                line = line_parts[0]

        # 去除包含中文字符的代码行
        if re.search('[\u4e00-\u9fff]', line):
            line = ""

        if '"""' in line or "'''" in line or "*/" in line:
            in_comment_block = not in_comment_block

        result.append(line)

    return '\n'.join(result)

# 指定要处理的文件夹路径
folder_path = "文件夹路径文件夹路径"
remove_chinese_code_and_comments_in_folder(folder_path)

```[/md]

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

gzsklsskszngc 发表于 2024-5-10 10:59
感谢分享!
cenoser795 发表于 2024-5-10 11:07
 楼主| Mis16800 发表于 2024-5-10 11:17
如果是上传代码到git就可以用来删除中文再上传
jyjjf 发表于 2024-5-10 12:17
或者可以加入中文字符转拼音或拼音首字母
VnYzm 发表于 2024-5-10 13:45
核心就是用[\u4e00-\u9fff]正则匹配中文字符的unicode编码
三滑稽甲苯 发表于 2024-5-10 15:01
可以用 r+ 实现读取并写入文件,没必要打开两次
 楼主| Mis16800 发表于 2024-5-11 09:24
三滑稽甲苯 发表于 2024-5-10 15:01
可以用 r+ 实现读取并写入文件,没必要打开两次

感谢你的建议,已经修改了
 楼主| Mis16800 发表于 2024-5-11 09:26
本帖最后由 Mis16800 于 2024-5-11 09:30 编辑
之前的代码
def remove_chinese_code_and_comments_in_folder(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file_name in files:
            if file_name.endswith((".java", ".js", ".py")):
                file_path = os.path.join(root, file_name)
                with open(file_path, "r", encoding="utf-8") as file:
                    code = file.read()

                cleaned_code = remove_chinese_code_and_comments(code)

                with open(file_path, "w", encoding="utf-8") as file:
                    file.write(cleaned_code)

修改后的代码
def remove_chinese_code_and_comments_in_folder(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file_name in files:
            if file_name.endswith((".java", ".js", ".py")):
                file_path = os.path.join(root, file_name)
                with open(file_path, "r", encoding="utf-8") as file:
                    code = file.read()

                    cleaned_code = remove_chinese_code_and_comments(code)

                    file.seek(0) # 将文件指针移动到文件的开头
                    file.truncate() #则会清空文件内容(从文件指针位置开始)
                    file.write(cleaned_code)


您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-7 20:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表