python 中提取文件的最后几列

 

001、

[root@PC1 test]# ls
a.txt  test.py
[root@PC1 test]# cat a.txt             ## 测试文件
001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060
[root@PC1 test]# cat test.py            ## 测试脚本
#!/usr/bin/python

in_file = open("a.txt", "r")
out_file = open("result.txt", "w")

for i in in_file:
        i = i.strip().split(" ")[:-3]
        out_file.write(" ".join(i) + "\n")

in_file.close()
out_file.close()
[root@PC1 test]# python test.py
[root@PC1 test]# ls
a.txt  result.txt  test.py
[root@PC1 test]# cat result.txt       ## 结果文件
001 002 003 004 005 006 007
011 012 013 014 015 016 017
021 022 023 024 025 026 027
031 032 033 034 035 036 037
041 042 043 044 045 046 047
051 052 053 054 055 056 057

 

 

posted @ 2022-11-04 12:07  小鲨鱼2018  阅读(167)  评论(0编辑  收藏  举报