简体   繁体   English

如何将 a.zip 文件中的多个.txt 文件合并到 Python 中的一个.txt 文件中?

[英]How to I merge multiple .txt files that are in a .zip file into only one .txt file in Python?

I'm trying to merge multiple.txt files that are in a.zip file into only one.txt file in Python.我正在尝试将 a.zip 文件中的多个.txt 文件合并到 Python 中的一个.txt 文件中。

My code is the following:我的代码如下:

firstfile = Path(r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip\AudioCaptureMemoryUsage_01_12_2022.txt')
secondfile = Path(r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip\AudioMatchingMemoryUsage_01_12_2022.txt')

newfile = input("Enter the name of the new file: ")
print()
print("The merged content of the 2 files will be in", newfile)

with open(newfile, "wb") as wfd:
    for f in [firstfile, secondfile]:
        with open(f, "rb") as fd:
            shutil.copyfileobj(fd, wfd, 1024 * 1024 * 10)

print("\nThe content is merged successfully.!")
print("Do you want to view it ? (y / n): ")

check = input()
if check == 'n':
    exit()
else:
    print()
    c = open(newfile, "r")
    print(c.read())
    c.close()



Thanks.谢谢。

I tried to merge them in only one file but it doesn't worked.我试图将它们合并到一个文件中,但没有成功。

To merge the files, you'll need to first extract the files from the zip file, then merge them, and then write the merged content to a new file.要合并文件,您需要先从 zip 文件中提取文件,然后合并它们,然后将合并的内容写入新文件。 Here is an example of how you can do this using the zipfile module.这是一个示例,说明如何使用 zipfile 模块执行此操作。

Update: If the.txt files are located inside a folder within the zip file, you'll need to include the folder name in the path when opening the files.更新:如果 .txt 文件位于 zip 文件中的文件夹内,您需要在打开文件时在路径中包含文件夹名称。

import zipfile

zip_file = r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip'
folder_name = 'myfolder'
first_file = folder_name + '/AudioCaptureMemoryUsage_01_12_2022.txt'
second_file = folder_name + '/AudioMatchingMemoryUsage_01_12_2022.txt'

with zipfile.ZipFile(zip_file, 'r') as zip_ref:
    with zip_ref.open(first_file) as f1, zip_ref.open(second_file) as f2:
        first_content = f1.read()
        second_content = f2.read()

    # Concatenate the two files
    merged_content = first_content + second_content
    
    # Write the merged content to a new file
    new_file = input("Enter the name of the new file: ")
    with open(new_file, 'wb') as new_f:
        new_f.write(merged_content)
        
    print("The content is merged successfully.!")
    print("Do you want to view it ? (y / n): ")

    check = input()
    if check == 'n':
        exit()
    else:
        print()
        c = open(new_file, "r")
        print(c.read())
        c.close()

Make sure to replace 'myfolder' with the actual name of the folder containing the.txt files in your zip file.确保将“myfolder”替换为 zip 文件中包含 .txt 文件的文件夹的实际名称。

For multiple files..对于多个文件..

import zipfile

zip_file = r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip'
folder_name = 'myfolder'
file_names = ['AudioCaptureMemoryUsage_01_12_2022.txt',
              'AudioMatchingMemoryUsage_01_12_2022.txt',
              'File3.txt',
              'File4.txt',
              ...
              'File29.txt']

merged_content = b''  # Initialize an empty bytes object
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
    for file_name in file_names:
        with zip_ref.open(folder_name + '/' + file_name) as f:
            merged_content += f.read()
            
    # Write the merged content to a new file
    new_file = input("Enter the name of the new file: ")
    with open(new_file, 'wb') as new_f:
        new_f.write(merged_content)
        
    print("The content is merged successfully.!")
    print("Do you want to view it ? (y / n): ")

    check = input()
    if check == 'n':
        exit()
    else:
        print()
        c = open(new_file, "r")
        print(c.read())
        c.close()
import os
import zipfile
import shutil

def extract_txt_files(zip_path, temp_folder):
    """Extracts all the .txt files from the given zip file to the given temp folder"""
    with zipfile.ZipFile(zip_path, "r") as zip_file:
        i = len([name for name in os.listdir(temp_folder) if name.endswith(".txt")]) + 1
        for member in zip_file.infolist():
            if member.filename.endswith(".txt"):
                zip_file.extract(member, temp_folder)
                os.rename(os.path.join(temp_folder, member.filename), os.path.join(temp_folder, f"{i}.txt"))
                i += 1

def merge_txt_files(temp_folder):
    """Merges all the .txt files from the given temp folder into a single file called "merged.txt" """
    with open("merged.txt", "w") as outfile:
        for filename in os.listdir(temp_folder):
            if filename.endswith(".txt"):
                with open(os.path.join(temp_folder, filename)) as infile:
                    outfile.write(infile.read())

def delete_temp_folder(temp_folder):
    """Deletes the given temp folder"""
    os.rmdir(temp_folder)

# paths to the zip files
zip1_path = "zip1.zip"
zip2_path = "zip2.zip"

# create a temporary folder to extract the .txt files
temp_folder = "temp"
os.makedirs(temp_folder, exist_ok=True)

# extract the .txt files from the zip files
extract_txt_files(zip1_path, temp_folder)
extract_txt_files(zip2_path, temp_folder)

# merge the .txt files
merge_txt_files(temp_folder)

# delete the temporary folder
shutil.rmtree(temp_folder)


print("The content is merged successfully.!")
    print("Do you want to view it ? (y / n): ")

    check = input()
    if check == 'n':
        exit()
    else:
        print()
        c = open(new_file, "r")
        print(c.read())
        c.close()

The zip path in the script is relative, which means that the zip files "zip1.zip" and "zip2.zip" are expected to be in the same directory as the script.脚本中的 zip 路径是相对路径,也就是说 zip 文件“zip1.zip”和“zip2.zip”应该和脚本在同一个目录下。

If the zip files contain multiple.txt files, the script will extract all of them to the temporary folder.如果 zip 文件包含多个.txt 文件,脚本会将它们全部提取到临时文件夹中。

the script renames the extracted.txt files with an incremental index and the.txt extension to ensure that all the extracted files will have unique names and not overwritten.This will maintain the order of txt files as they were in zip file.该脚本使用增量索引和 .txt 扩展名重命名提取的 .txt 文件,以确保所有提取的文件都具有唯一的名称并且不会被覆盖。这将保持 txt 文件在 zip 文件中的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM