Python提取文本文件中含有指定字符的行并保存

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/11/23 21:47
# @Author  : Qiufen.Chen
# @Email   : 1760812842@qq.com
# @File    : extract_H.py
# @Software: PyCharm

"""
purpose:提取一行中第二列为 H 的行,保存输出
例如:
Q    1    31        9.626     22.325    20.619
Q    1    32        6.050     21.718    19.510
W    1    33        7.220     18.941    17.225
L    1    34        9.325     17.198    19.804
D    1    35        6.477     17.583    22.194
H    1    36        4.100     15.997    19.773
F    1    37        6.591     13.232    19.112
V    H    38        7.021     12.387    22.781
L    H    39        3.301     12.732    23.268
Y    H    40        2.610     9.784     20.988
I    H    41        5.250     7.670     22.659
I    H    42        3.852     8.253     26.160
T    H    43        0.266     7.917     24.985
A    H    44        1.169     4.614     23.367
V    H    45        2.812     3.486     26.557
T    H    46        -0.171    4.648     28.552
I    H    47        -2.551    2.822     26.253
F    H    48        -0.387    -0.283    26.485
V    H    49        -0.567    -0.623    30.281
C    H    50        -4.163    0.438     30.062
L    H    51        -4.778    -2.635    27.883
L    H    52        -2.860    -5.059    30.075
L    H    53        -4.934    -3.817    32.998
L    H    54        -8.122    -4.210    31.050
I    H    55        -7.086    -7.761    30.248
C    H    56        -6.390    -8.563    33.910
I    H    57        -9.815    -7.195    34.688
V    H    58        -11.671   -9.325    32.154
R    2    59        -9.633    -12.522   31.893
F    2    60        -8.192    -12.626   35.431
N    2    61        -10.984   -11.888   37.896
R    2    62        -11.666   -14.628   40.476
R    2    63        -14.927   -15.402   38.657
A    2    64        -13.198   -15.857   35.277

提取出含有 H 的行:
V    H    38        7.021     12.387    22.781
L    H    39        3.301     12.732    23.268
Y    H    40        2.610     9.784     20.988
I    H    41        5.250     7.670     22.659
I    H    42        3.852     8.253     26.160
T    H    43        0.266     7.917     24.985
A    H    44        1.169     4.614     23.367
V    H    45        2.812     3.486     26.557
T    H    46        -0.171    4.648     28.552
I    H    47        -2.551    2.822     26.253
F    H    48        -0.387    -0.283    26.485
V    H    49        -0.567    -0.623    30.281
C    H    50        -4.163    0.438     30.062
L    H    51        -4.778    -2.635    27.883
L    H    52        -2.860    -5.059    30.075
L    H    53        -4.934    -3.817    32.998
L    H    54        -8.122    -4.210    31.050
I    H    55        -7.086    -7.761    30.248
C    H    56        -6.390    -8.563    33.910
I    H    57        -9.815    -7.195    34.688
V    H    58        -11.671   -9.325    32.154

最后保存
"""

import os

file_dir = '/home/cqfnenu/LSTM/data/atom_data/'
save_dir = '/home/cqfnenu/LSTM/data/atom_data_new/'

file_num = 0
for (root, dirs, files) in os.walk(file_dir):
    for file_name in files:
        file_num = file_num + 1
        new_list = []
        with open(os.path.join(root, file_name), 'r') as f1:
            lines = f1.readlines()
            for line in lines:
                if str(line)[5] == 'H':
                    new_list.append(line)
            with open(save_dir + file_name, 'w') as outfile:
                for item in new_list:
                    outfile.write(item)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值