python中提取指定列的数据

 

1、提取第一列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt             ## 测试数据
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py           ## 提取程序
#!/usr/bin/python

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

lines = in_file.readlines()

for i in lines:
        temp = i.split()
        out_file.write(temp[1 - 1])
        out_file.write("\n")

in_file.close()
out_file.close()

[root@PC1 test2]# python test.py        ## 执行程序
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt        ## 提取结果
01
11
21
31
复制代码

 

 

2、提取第三列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    temp = i.split()
    out_file.write(temp[3 - 1])
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
03
13
23
33
复制代码

 

 

3、提取1-3列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    temp = i.split()
    #temp = " ".join(temp[:3])
    out_file.write(" ".join(temp[:3]))
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
01 02 03
11 12 13
21 22 23
31 32 33
复制代码

 

 

4、提取1、3、7列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

import numpy as np
in_file = open("a.txt", "r")
out_file = open("result.txt", "w")
lines = in_file.readlines()

for i in lines:
    temp = i.split()
    np_temp = np.array(temp)
    temp2 = list(np_temp[[1 - 1, 3 - 1, 7 - 1]])
    temp3 = " ".join(temp2)
    out_file.write(temp3)
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
01 03 07
11 13 17
21 23 27
31 33 37
复制代码

 

 

 5、提取最后一列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    i = i.split()
    out_file.write(i[-1])
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
10
20
30
40
复制代码

 

 

6、提取倒数第3列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    temp = i.split()
    out_file.write(temp[-3])
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
08
18
28
38
复制代码

 

 

7、输出最后5列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    temp = i.split()
    temp2 = " ".join(temp[-5:])
    out_file.write(temp2)
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
06 07 08 09 10
16 17 18 19 20
26 27 28 29 30
36 37 38 39 40
复制代码

 

 

8、所有列逆向输出

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python

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

for i in lines:
    temp = i.split()
    temp2 = " ".join(temp[::-1])
    out_file.write(temp2)
    out_file.write("\n")

in_file.close()
out_file.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  result.txt  test.py
[root@PC1 test2]# cat result.txt
10 09 08 07 06 05 04 03 02 01
20 19 18 17 16 15 14 13 12 11
30 29 28 27 26 25 24 23 22 21
40 39 38 37 36 35 34 33 32 31
复制代码

 

9、输出奇数列、偶数列

复制代码
[root@PC1 test2]# ls
a.txt  test.py
[root@PC1 test2]# cat a.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
[root@PC1 test2]# cat test.py
#!/usr/bin/python
in_file = open("a.txt", "r")
out_file1 = open("odd.txt", "w")
out_file2 = open("even.txt", "w")
lines = in_file.readlines()
for i in lines:
    temp = i.split()
    odd =" ".join(temp[::2])
    even =" ".join(temp[1::2])
    out_file1.write(odd)
    out_file1.write("\n")
    out_file2.write(even)
    out_file2.write("\n")
in_file.close()
out_file1.close()
out_file2.close()
[root@PC1 test2]# python test.py
[root@PC1 test2]# ls
a.txt  even.txt  odd.txt  test.py
[root@PC1 test2]# cat odd.txt
01 03 05 07 09
11 13 15 17 19
21 23 25 27 29
31 33 35 37 39
[root@PC1 test2]# cat even.txt
02 04 06 08 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
复制代码

 

posted @   小鲨鱼2018  阅读(7920)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-06-02 c语言中strncp函数,函数原型、头文件
2021-06-02 c语言中strcpy函数,函数原型、头文件
2021-06-02 c语言 11-6
2021-06-02 c语言 11-5
2021-06-02 c语言 11-4
2021-06-02 c语言中求字符串的长度
2021-06-02 c语言 11-3
点击右上角即可分享
微信分享提示