【问题标题】:regex matching of numbers and redirecting them to different output files正则表达式匹配数字并将它们重定向到不同的输出文件
【发布时间】:2016-07-11 00:33:48
【问题描述】:

这是我当前的日志文件

event_1.log

a   0   3.2 1024    1   0   0
a   0   6.4 2048    2   0   0
le  0   9.6 2048    2   0   0
a   0   12.8    2048    2   0   0
le  0   12.8    2048    2   0   0
ll  0   19.6    2048    2   0   0
a   1   19.6    1024    1   0   0
a   1   22.4    3072    3   0   0
d   0   19.2    2048    2   0   0
le  1   22.4    2048    2   0   0
ll  1   22.8    2048    2   0   0
d   1   22.8    1024    1   0   0
a   0   26  2048    2   0   0

基于第二列,我需要创建文件名 {second_column}.log。我只需要提取具有 first_column 等于 a 或 d。其他的(le 和 ll)应该被跳过。

以下是我的预期输出

0.log

a   0   3.2 1024    1   0   0
a   0   6.4 2048    2   0   0
a   0   12.8    2048    2   0   0
d   0   19.2    2048    2   0   0
a   0   26  2048    2   0   0

1.log

a   1   19.6    1024    1   0   0
a   1   22.4    3072    3   0   0
d   1   22.8    1024    1   0   0

这是我尝试过的,但我显然是正则表达式的新手。我对其他解决方案(shell、sed、awk 等)持开放态度。

import re

input_file = open("event_1.log", "r")
output_file = open("column2.log", "w") # want this to be the name of the 2nd column  
for line in input_file:
    match_defines = re.match(r'\s*([a-z]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', line)

    if match_defines.group(1) == 'a':
        newline1= "\ndef %s():\n    return %s" % (match_defines.group(1),match_defines.group(2))
        output_file.write(newline1)

    else:
        output_file.write(line)

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: python regex shell awk sed


    【解决方案1】:
    cat event_1.log | grep "^[a|d]" | while read l; do i=`echo $l |awk '{print $2}'`; echo $l >> ${i}.log; done
    

    【讨论】:

      【解决方案2】:

      您可以使用pandasregex 在第一列中检查ad

      import pandas as pd
      
      df = pd.read_table("event_1.log", header=None)
      
      df[df[1]==1 & (df[0].str.contains('^(a|d)$'))].to_csv('1.log')
      
      #    0  1     2     3  4  5  6
      #6   a  1  19.6  1024  1  0  0
      #7   a  1  22.4  3072  3  0  0
      #11  d  1  22.8  1024  1  0  0
      
      df[(df[1]==0) & (df[0].str.contains('^(a|d)$'))].to_csv('0.log')
      
      #Out[99]:
      #    0  1     2     3  4  5  6
      #0   a  0   3.2  1024  1  0  0
      #1   a  0   6.4  2048  2  0  0
      #3   a  0  12.8  2048  2  0  0
      #8   d  0  19.2  2048  2  0  0
      #12  a  0  26.0  2048  2  0  0
      

      数据:

      In [113]: df
      Out[113]:
           0  1     2     3  4  5  6
      0    a  0   3.2  1024  1  0  0
      1    a  0   6.4  2048  2  0  0
      2   le  0   9.6  2048  2  0  0
      3    a  0  12.8  2048  2  0  0
      4   le  0  12.8  2048  2  0  0
      5   ll  0  19.6  2048  2  0  0
      6    a  1  19.6  1024  1  0  0
      7    a  1  22.4  3072  3  0  0
      8    d  0  19.2  2048  2  0  0
      9   le  1  22.4  2048  2  0  0
      10  ll  1  22.8  2048  2  0  0
      11   d  1  22.8  1024  1  0  0
      12   a  0  26.0  2048  2  0  0
      

      【讨论】:

      • 我是熊猫新手。任何想法,重定向如何在 pandas 中工作
      • 真的很简单,我更新了我的帖子:你需要通过传递文件名来调用你的dataFrame上的to_csv方法。 (以及其他一些参数,如标题、分隔符等)。您的数据将保存在这些文件中。顺便说一句,与经过打开/关闭过程的 input_file / output_file 相比,它非常紧凑和整洁。
      • 谢谢。当我运行上述内容时,出现以下错误。 AttributeError: 'module' 对象没有属性 'read_table' 。
      • 对不起,我的文件命名是一个愚蠢的错误。但我仍然无法运行您的代码。它在第 5 行引发 KeyError: 'no item named 1'
      • df 被正确读取了吗?你的df 格式是否与我在数据部分中的格式相同(可能是标题,例如列的名称不同,所以在这种情况下你应该调整你的代码 - 在我这边我写了df[0],因为我的第一列被命名0).
      【解决方案3】:
      import re
      with open("event_1.log") as f,open("0.log","w+") as f0, open("1.log","w+") as f1:
          for line in f:
              result1=re.findall(r"^[ad]\s+0\s.*$",line,re.S)
              result2=re.findall(r"^[ad]\s+1\s.*$",line,re.S)
              if result1:
                  f0.writelines(result1)
              if result2:
                  print(result2)
                  f1.writelines(result2)
      

      或:

      with open("event_1.log") as f,open("0.log","w+") as f0, open("1.log","w+") as f1:
          for line in f:
              sl=line.split()
              result0=sl[1]=="0" and (sl[0]=="a" or sl[0]=="d")
              result1=sl[1]=="1" and (sl[0]=="a" or sl[0]=="d")
              if result0:
                  f0.write(line)
                  print(line)
              if result1:
                  f1.write(line)
      

      列表理解:

      with open("event_1.log") as f,open("0.log","w+") as f0, open("1.log","w+") as f1:
          isad=lambda x:x.split()[0] in ['a','d'] and x.split()[1] in ['0','1']
          [f0.write(r) if r.split()[1]=='0' else f1.write(r) for r in f if isad(r)]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 2013-11-19
        • 2020-10-04
        • 2010-12-19
        • 1970-01-01
        相关资源
        最近更新 更多