Skip to content

Instantly share code, notes, and snippets.

@baichen99
Created August 2, 2022 12:46
Show Gist options
  • Save baichen99/afa3b8c0e14a53553b8697989cfe2b40 to your computer and use it in GitHub Desktop.
Save baichen99/afa3b8c0e14a53553b8697989cfe2b40 to your computer and use it in GitHub Desktop.
extract data from a table in python
# https://stackoverflow.com/questions/16036337/regular-expression-to-extract-data-from-a-table-in-python
def gen_regex(title, col_num, start_with_space=False):
if start_with_space:
r = f'{title}\n((?:(?:[\s]+[^\s]+[\s]+){{{col_num-1}}}[^\s]+\n)+)'
else:
r = f'{title}\n((?:(?:[^\s]+[\s]+){{{col_num-1}}}[^\s]+\n)+)'
return r
import re
with open('test.txt', 'w') as f:
s = f.read()
regex1 = gen_regex('ATOMIC_SPECIES', col_num=3, start_with_space=True)
pattern1 = re.compile(regex)
res_list1 = pattern.findall(s)
print(res_list1)
regex2 = gen_regex('ATOMIC_POSITIONS \(crystal\)', col_num=4)
pattern2 = re.compile(regex)
res_list2 = pattern.findall(s)
print(res_list2)
&CONTROL
calculation='vc-relax'
title='Ag_core_13_thr'
prefix='Ag_core_13_thr'
verbosity='high'
restart_mode='from_scratch'
tprnfor=.true.
outdir='.'
pseudo_dir='/PARA/grid138/liunianhong/Pseudo'
tstress=.true.
forc_conv_thr=1.0d-4
etot_conv_thr=1.0d-7
/
&SYSTEM
ibrav = 0,
nat = 28,
ntyp = 2,
ecutwfc = 80.0 ,
ecutrho = 960.0 ,
nosym=.TRUE.,
input_DFT = 'PBE' ,
occupations = 'smearing' ,
degauss = 1.0d-4 ,
smearing = 'marzari-vanderbilt' ,
/
&ELECTRONS
electron_maxstep = 200,
conv_thr = 1.0d-7 ,
mixing_mode = 'plain' ,
mixing_beta = 0.3d0 ,
/
&IONS
ion_dynamics='bfgs'
/
&CELL
press_conv_thr = 0.5D0,
press = 0.D0,
cell_dynamics = 'bfgs',
cell_dofree= '2Dxy',
/
ATOMIC_SPECIES
Ag 107.87 Ag.pbe-spn-rrkjus_psl.1.0.0.UPF
Au 196.67 Au.pbe-spn-rrkjus_psl.1.0.0.UPF
CELL_PARAMETERS (angstrom)
5.023250764 2.900444827 0.000000000
5.023317586 -2.900714647 0.000000000
0.000000000 0.000000000 50.000000000
ATOMIC_POSITIONS (crystal)
Ag 0.166342087 0.166239670 0.355560155
Ag 0.666410995 0.666104546 0.355499555
Ag 0.166411014 0.666104487 0.355499561
Ag 0.666342012 0.166239660 0.355560152
Ag 0.333132001 0.332885667 0.403788057
Ag 0.833131920 0.332885879 0.403788055
Ag 0.833347162 0.832460241 0.403580911
Ag 0.333347479 0.832460258 0.403580914
Ag 0.500177122 -0.000805369 0.451462818
Ag 0.500043058 0.499456656 0.451675040
Ag 0.000042887 0.499456624 0.451675039
Ag 0.000177106 -0.000805242 0.451462822
Ag 0.166665954 0.166667791 0.499999265
Au 0.666666380 0.666666915 0.499999436
Au 0.166666387 0.666666912 0.499999436
Ag 0.666665948 0.166667795 0.499999266
Ag 0.333155454 0.334140114 0.548536742
Ag 0.833155432 0.334140235 0.548536746
Ag 0.833290054 0.833877144 0.548324243
Ag 0.333290224 0.833877168 0.548324244
Ag 0.500201452 0.000447638 0.596212189
Ag 0.499985888 0.500873894 0.596419553
Ag -0.000014419 0.500873875 0.596419551
Ag 0.000201381 0.000447847 0.596212186
Ag 0.166991617 0.167093623 0.644440642
Ag 0.666922630 0.667228900 0.644501179
Ag 0.166922614 0.667228952 0.644501174
Ag 0.666991683 0.167093627 0.644440644
K_POINTS automatic
13 13 1 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment