Python,读取压缩包

import zipfile

import re

# 压缩包路径和密码

zip_path = 'archive.zip'

password = ''

# 要查找的中文词

target_words = ['词1', '词2']

# 正则表达式模式,用于匹配中文

chinese_pattern = re.compile('[\u4e00-\u9fa5]+')

# 打开压缩包

with zipfile.ZipFile(zip_path, 'r') as zip_ref:

    # 解压文件

    for filename in zip_ref.namelist():

        if filename.endswith('.txt'):

            # 读取TXT文件内容

            with zip_ref.open(filename) as file:

                content = file.read().decode('utf-8')

                

            # 在文件内容中查找匹配的中文词

            for target_word in target_words:

                matches = chinese_pattern.findall(content)

                if matches:

                    print(f"Found '{target_

import zipfile

import re

# 压缩包路径和密码

zip_path = 'archive.zip'

password = ''

# 要查找的中文词

target_words = ['词1', '词2']

# 正则表达式模式,用于匹配中文

chinese_pattern = re.compile('[\u4e00-\u9fa5]+')

# 打开压缩包

with zipfile.ZipFile(zip_path, 'r') as zip_ref:

    # 解压文件

    for filename in zip_ref.namelist():

        if filename.endswith('.txt'):

            # 读取TXT文件内容

            with zip_ref.open(filename) as file:

                content = file.read().decode('utf-8')

                

            # 在文件内容中查找匹配的中文词

            for target_word in target_words:

                matches = chinese_pattern.findall(content)

                if matches:

                    print(f"Found '{target_word}' in file '{filename}': {matches}")

word}' in file '{filename}': {matches}")

 

import zipfile

import re

# 压缩包路径和密码

zip_path = 'archive.zip'

password = ''

# 要查找的中文词

target_words = ['词1', '词2']

# 正则表达式模式,用于匹配中文

chinese_pattern = re.compile('[\u4e00-\u9fa5]+')

# 打开压缩包

with zipfile.ZipFile(zip_path, 'r') as zip_ref:

    # 解压文件

    total_word_count = 0 # 用于记录目标词的总数

    for filename in zip_ref.namelist():

        if filename.endswith('.txt'):

            # 读取TXT文件内容

            with zip_ref.open(filename) as file:

                content = file.read().decode('utf-8')

            # 在文件内容中查找匹配的中文词

            for target_word in target_words:

                matches = chinese_pattern.findall(content)

                if matches:

                    total_word_count += len(matches) # 增加目标词的总数

                    print(f"Found '{target_word}' in file '{filename}': {matches}")

    print(f"Total count for '{target_words[0]}' and '{target_words[1]}' in archive: {total_word_count}")

import zipfile

import re

# 压缩包路径和密码

zip_path = 'archive.zip'

password = ''

# 要查找的中文词

target_words = ['词1', '词2']

# 正则表达式模式,用于匹配中文

chinese_pattern = re.compile('[\u4e00-\u9fa5]+')

# 打开压缩包

with zipfile.ZipFile(zip_path, 'r') as zip_ref:

    # 解压文件

    total_word_count = 0 # 用于记录目标词的总数

    for filename in zip_ref.namelist():

        if filename.endswith('.txt'):

            # 读取TXT文件内容

            with zip_ref.open(filename) as file:

                content = file.read().decode('utf-8')

            # 在文件内容中查找匹配的中文词

            for target_word in target_words:

                matches = chinese_pattern.findall(content)

                if matches:

                    total_word_count += len(matches) # 增加目标词的总数

                    print(f"Found '{target_word}' in file '{filename}': {matches}")

    print(f"Total count for '{target_words[0]}' and '{target_words[1]}' in archive: {total_word_count}")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值