我怎么能在python(awk示例提供)文件分隔符中执行此代码

时间:2012-08-01 12:21:36

标签: python python-3.x

如果我想将以下awk代码作为python代码执行,那么前进的最佳方法是什么?如果我要使用列表,我将如何弹出列表,以便最终我可以将字段1和3打印回输出列表并将其写入文件。

感谢 格雷厄姆

AWK 

#!/bin/awk -f
BEGIN {
        FS=":";
}
{
                print $1,$3
        }

测试文件

Error code 27: This is error code 27:Ihave no comment here
Error code 24: This is error code 27:Ihave no comment here
Error code 27: This is error code 27:Ihave no comment here
Error code 26: This is error code 27:Ihave no comment here
Error code 27: This is error code 27:Ihave no comment here
Error code 29: This is error code 27:Ihave no comment here
Error code 01: This is error code 27:Ihave no comment here

1 个答案:

答案 0 :(得分:1)

f = open(file_name)
for line in f:
  s = line.split(':')
  print s[0],s[2]