【问题标题】:python read -> analyze -> print multiple filespython读取->分析->打印多个文件
【发布时间】:2014-03-24 02:31:28
【问题描述】:

我有 6 个格式相似但名称不同的文件。 (例如file_AA.dat file_AB.dat file_AC.dat file_BA.dat file_BB.dat file_BC.dat)

我可以编写一个 for 循环脚本来一次读取、分析和打印这些文件,而不是运行脚本 6 次吗?比如,

for i in {AA AB AC BA BB BC} 
 filename = 'file_$i.dat'
 file = open (filename, 'r')
 Do a lot, lot of analysis for lots of rows and columns :P 
 file open('output_file_$i.dat','w')
 Do some for loop for writing and calculation 
file.close

所以,我希望一次自动化读取/分析/写入不同文件(但格式相似)的过程。我很好奇如何处理其输入/输出部分的命名。这样,我希望我可以更快速、更轻松地分析大量文件。

或者,有什么方法可以使用 python 和 Cshell 或 shell 脚本的混合来做同样的事情?

谢谢

【问题讨论】:

    标签: python shell


    【解决方案1】:

    想法是遍历文件名,循环打开每个文件,进行分析,然后编写输出文件:

    filenames = ['file_AA.dat', 'file_AB.dat', 'file_AC.dat', 'file_BA.dat', 'file_BB.dat', 'file_BC.dat']
    
    for filename in filenames:
        with open(filename, 'r') as input_file:
            # Do a lot, lot of analysis for lots of rows and columns :P
    
        with open('output_%s' % filename, 'w') as output_file:
            # Do some for loop for writing and calculation
    

    请注意,在处理文件时建议使用with statement

    另请注意,您可以将两个 with 语句合二为一,请参阅:

    UPD:您可以使用string formatting 来构建文件名列表:

    >>> patterns = ['AA', 'AB', 'AC', 'BA', 'BB', 'BC']
    >>> filenames = ['file_{}.dat'.format(pattern) for pattern in patterns]
    >>> filenames
    ['file_AA.dat', 'file_AB.dat', 'file_AC.dat', 'file_BA.dat', 'file_BB.dat', 'file_BC.dat']
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:
      files = [
          "file_AA.dat",
          "file_AB.dat",
          "file_AC.dat",
          "file_BA.dat",
          "file_BB.dat",
          "file_BC.dat",
      ]
      for filename in files:
          f = open(filename)
          data = f.read() #reads all data from file into a string
          #parse data here and do other stuff
          output = open("output_"+filename, 'w')
          output.write(junk) #junk is a string that you shove the results into
          output.close()
      

      如果您有大量文件并且您正在对文件中的数据进行大量计算分析,您可以使用multiprocessing 模块。至于 bash 与 python,我基本上使用 python 解释器,就像很多人使用 bash shell 一样,我几乎没有理由离开 python 解释器。此外,如果这些文件是目录中的唯一文件,您可以使用 os 模块遍历目录。如果必须在 bash shell 中运行程序,可以使用 subprocess 模块。

      【讨论】:

        【解决方案3】:

        您可以使用列表推导干净地做到这一点:

        for filein, fileout in [('file_%s.dat' % x, 'out_%s.dat' %x) for x in ('AA','AB','AC', 'BA', 'BB', 'BC')]:
            with open(filein, 'rb') as fp, open(fileout,'w') as fpout:
                # Read from fp, write to fpout as needed
        

        此列表推导式创建输入/输出文件对列表:

        [('file_%s.dat' % x, 'out_%s.dat' %x) for x in ('AA','AB','AC', 'BA', 'BB', 'BC')]
        

        这会生成一个如下所示的列表:

        [('file_AA.dat', 'out_AA.dat'), ('file_AB.dat', 'out_AB.dat') ...]
        

        您可以尝试测试它是如何工作的:

        lst = [('file_%s.dat' % x, 'out_%s.dat' %x) for x in ('AA','AB','AC', 'BA', 'BB', 'BC')]:
        print lst
        
        for filein, fileout in lst:
            with open(filein, 'rb') as fp, open(fileout,'w') as fpout:
                # Read from fp, write to fpout as needed
        

        【讨论】:

        • 一次分析多部作品的绝妙策略。我也会试试这个。非常感谢
        猜你喜欢
        • 2017-11-18
        • 2022-08-13
        • 2019-05-07
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 2017-06-20
        • 2015-04-29
        相关资源
        最近更新 更多