【问题标题】:Elegant way of combining files - Python优雅的文件组合方式——Python
【发布时间】:2016-05-03 13:29:19
【问题描述】:

您好,只是一个简单的问题。我有一个可以完美运行的文件串联,但它有点乱。我想知道是否有更优雅的方式来写这个:

path = path/to/file/location
with open(path + 'result.txt', 'w') as result, \
        open(path + 'file1.txt') as f1, \
            open(path + 'file2.txt' ) as f2, \
                open(path + 'file3.txt' ) as f3, \
                    open(path + 'file4.txt' ) as f4, \
                        open(path + 'file5.txt' ) as f5, \
                            open(path + 'file6.txt' ) as f6, \
                                open(path + 'file7.txt' ) as f7, \
                                    open(path + 'file8.txt' ) as f8, \
                                        open(path + 'file9.txt' ) as f9, \
                                            open(path + 'file10.txt' ) as f10, \
                                                open(path + 'file11.txt' ) as f11, \
                                                    open(path + 'file12.txt' ) as f12, \
                                                        open(path + 'file13.txt' ) as f13, \
                                                            open(path + 'file14.txt' ) as f14, \
                                                                open(path + 'file15.txt' ) as f15, \
                                                                    open(path + 'file16.txt' ) as f16:
    for line1, line2, line3, line4, line5, line6, line7, line8, \ 
        line9, line10, line11, line12, line13, line14, line15, line16 \
        in zip(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16):

        result.write('{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
        {}, {}, {}\n'.format(line1.rstrip(), line2.rstrip(), line3.rstrip(), line4.rstrip(), \
        line5.rstrip(), line6.rstrip(), line7.rstrip(), line8.rstrip(), line9.rstrip(), \
        line10.rstrip(), line11.rstrip(), line12.rstrip(), line13.rstrip(), line14.rstrip(), \
        line15.rstrip(), line16.rstrip()))

谢谢

【问题讨论】:

  • 不确定是否可以使用with,但您当然可以将文件名放在列表中(或仅使用range)和open.close 手动将文件放入一个循环。
  • 啊抱歉它是函数的一部分,所以 with 在我的脚本中有效。这只是有趣的一点。
  • 对于with,您可以使用contextlib.ExitStack(如果您使用的是Python 3),在我由dan-man 链接的答案中也有描述。其余的你需要使用列表和循环。
  • @ben - 您可能需要编辑此问题的标题以使其更具体地适用于您的用例:“连接”并不是描述您正在做什么的非常有用的方式。

标签: python concatenation


【解决方案1】:

你可以在没有with 的情况下执行此操作,将文件放在一个列表中,然后在所有操作完成后手动循环关闭它们。这也将使format 行更简单:

path = "path/to/file/location/"
with open(path + 'result.txt', 'w') as result:
    files = [open(path + 'file%d.txt' % (n+1)) for n in range(16)]
    form = ", ".join('{}' for f in files) + '\n'
    for lines in zip(*files):
        result.write(form.format(*map(str.rstrip, lines)))
    for f in files:
        f.close()

或使用contextlib.ExitStack,如 cmets 中所建议的那样。这样,打开的文件将被传递给stack,这将负责在with 块之后关闭文件。

with open(path + 'result.txt', 'w') as result, contextlib.ExitStack() as stack:
    files = [stack.enter_context(open(path + 'file%d.txt' % (n+1))) for n in range(16)]
    form = ", ".join('{}' for f in files) + '\n'
    for lines in zip(*files):
        result.write(form.format(*map(str.rstrip, lines)))

【讨论】:

    【解决方案2】:

    您可以按顺序处理文件(这解决了一次打开太多文件的潜在问题):

    result_names = ['result1','result2']
    result_index = 0
    old_result_path = path + "file1.txt"
    for n in xrange(2,17):
        new_result_path = path + (result_names[result_index] if n<16 else 'result.txt')
        input_path = path + "file%d.txt" % n
        with open( old_result_path, 'r' ) as old_input, \
            open( input_path, 'r' ) as new_input, 
                open( new_result_path, 'w' ) as result:
           for line1, line2 in zip( old_input, new_input ):
               result.write('{}, {}\n'.format(line1.rstrip(), line2.rstrip())
        old_result_path = new_result_path
        result_index = 1 - result_index
    

    这会让result1.txtresult2.txt 撒谎,你可能关心也可能不关心清理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      相关资源
      最近更新 更多