如何重新排列长文本文件中的行?

问题描述 投票:0回答:1

我有一个包含以下信息的大文件:

15
dist_0.185_0.245_0.320/metad_3_t3745_0.198_0.260_0.326.xyz
C         -1.79467        0.35800       -0.28800
H         -1.21467        0.50800       -1.26800
H         -2.37467       -0.52200       -0.38800
S         -0.71467        0.08800        1.10200
C          1.04533        2.63800        1.08200
H          2.10533        2.84800        0.96200
H          0.47533        3.26800        0.42200
S          1.07533        0.78800        0.63200
C          0.60533       -2.93200       -0.87800
H          1.26533       -3.82200       -0.90800
H         -0.02467       -2.96200        0.00200
S          1.50533       -1.33200       -0.80800
H         -2.44467        1.20800       -0.08800
H          0.64533        2.91800        2.09200
H         -0.15467       -3.05200       -1.66800
15
dist_0.185_0.245_0.335/metad_3_t3664_0.196_0.259_0.342.xyz
C         -2.03000        0.44267        0.23400
H         -1.36000        0.19267       -0.59600
H         -2.63000       -0.37733        0.38400
S         -0.84000        0.41267        1.56400
C          1.17000        2.62267        1.11400
H          2.24000        2.79267        1.01400
H          0.70000        3.24267        0.48400
S          0.86000        0.80267        0.66400
C          0.75000       -2.97733       -1.48600
H          1.68000       -3.32733       -1.91600
H          0.48000       -3.59733       -0.64600
S          0.82000       -1.21733       -0.94600
H         -2.66000        1.33267        0.21400
H          0.86000        2.93267        2.13400
H  
...

总长度约为140000行。这里的原子排列为C,H,H,S,C,H,H,S,C,H,H,S,H,H,H,但我想按以下方式排列它们:C,S ,H,H,H,C,S,H,H,H,C,S,H,H,H。如何使用Python或Shell排列整个文件?

python shell line
1个回答
0
投票
import re pattern = r"(C|H|S)\s+-?\d.\d+\s+-?\d.\d+\s+-?\d.\d+" with open(yourfile) as f: data = re.findall(pattern, line) for i in range(len(data) // 15)): chunk = data[i:(i+15)] reorder(chunk) write_to_file(chunk) def reorder(chunk): # reorder to your liking by the first letter of each element in the list def write_to_file(chunk): #write to a new file in the same format

我跳过了reorderwrite_to_file函数的实现,因为它们并不难实现

© www.soinside.com 2019 - 2024. All rights reserved.