【问题标题】:Python - merge multiple files based on file prefixPython - 基于文件前缀合并多个文件
【发布时间】:2020-07-23 04:17:52
【问题描述】:

Python 2.7

我有多个文件:

文件 A_01.txt 文件内容:aaaa

文件 A_02.txt 文件内容:bbbb

文件 B_01.txt 文件内容:aaaB

文件 B_02.txt 文件内容:bbbB

文件 D_01.txt 文件内容:aaaD

文件 D_02.txt 文件内容:bbbD

我需要根据文件前缀创建“合并”文件,

对于以 A_0 开头的文件,创建合并文件merged_A.txt 并将所有文件的内容以 A_0 开头,

merged_B.txt 用于以 B_ 开头的文件

所有文件都一样

# get all files in folder 

files = os.listdir("C:\\MTA\\mta") 

for filename in files:
    #get prefix
   prefix = filename[0:3]

# open destination file to merge individual files into 

   with open(os.path.join("C:\\MTA\mta", "merged" + "_" + prefix + ".txt"), 'w') as outfile:
       # go through all files and merge it into outfile
       for file in files:
           with open(os.path.join("C:\\MTA\mta", filename)) as infile:
             outfile.write(infile.read())
           outfile.write("--------------\n")

以上代码,生成合并文件,但是,两个合并文件都包含所有文件的内容

files = os.listdir("C:\\MTA\\mta") 

for filename in files:
    #get prefix
   prefix = filename[0:3]

# open destination file to merge individual files into 

   with open(os.path.join("C:\\MTA\mta", prefix + "file.siem"), 'w') as outfile:
       # go through all files and merge it into outfile
       #for filename in files:
           with open(os.path.join("C:\\MTA\mta", filename)) as infile:
             outfile.write(infile.read())
           outfile.write("--------------\n")

此版本只将一个文件的内容写入合并文件中

【问题讨论】:

  • 可能你想在打开输出文件时使用w+而不是w
  • 不,那没用

标签: python join concat


【解决方案1】:

每次读取文件时,您都在写入一个新文件,您需要改为追加。您还有一个不必要的嵌套 for 循环来读取文件,而您可以在外部循环中读取它们。这应该有效:

import os

# get all files in folder 

files = os.listdir("C:\\MTA\\mta") 

for filename in files:
    #get prefix
    prefix = filename[0:2]


# open destination file to merge individual files into 

    with open(os.path.join("C:\\MTA\\mta", "merged" + "_" + prefix + ".txt"), 'a') as outfile:
       # go through all files and merge it into outfile
        with open(os.path.join("C:\\MTA\\mta", filename)) as infile:
            outfile.write(infile.read())
        outfile.write("--------------\n")

【讨论】:

  • ahhh...初学者的错误,是的,它现在工作正常,是的,我注意到我可以摆脱内循环,我在我的代码的第二个版本中做到了,谢谢
  • 还有一件事:要合并的文件总共是30MB,而目标文件是214MB,可以减少吗?
  • 想通了outfile.writelines(infile)
猜你喜欢
  • 2021-09-27
  • 1970-01-01
  • 2014-10-03
  • 2013-07-01
  • 2017-08-29
  • 2019-07-13
  • 1970-01-01
  • 2014-12-02
  • 2015-11-07
相关资源
最近更新 更多