python - Reading in specific data from a text file -




i trying read in specific data text file 10-10-1cnt_pot.pot_fmt. data needed a,b , c fft coefficients (25,300,300) in case. way can think of read these in, position in text file. not prone bugs if text file changes slightly. can suggest alternate method?

please see example text file below (and buggy code):

begin header         real lattice(a)               lattice parameters(a)    cell angles    2.4675850   0.0000000   0.0000000     =    2.467585  alpha =   90.000000    0.0000000  30.0000000   0.0000000     b =   30.000000  beta  =   90.000000    0.0000000   0.0000000  30.0000000     c =   30.000000  gamma =   90.000000   1                            ! nspins 25   300   300                ! fine fft grid along <a,b,c> end header: data "<a b c> pot" in units of hartrees 

code:

file = open("10-10-1cnt_pot.pot_fmt", 'r') lines = file.readlines() file.close()   parts = lines[3].split()  = parts[5]  parts1 = lines[4].split()  b = parts1[5]  parts2 = lines[5].split() c = parts2[5]  parts3 = lines[8].split() width = parts3[0]  parts4 = lines[8].split() height = parts4[1]  parts5 = lines[8].split() depth = parts5[2] 

you need use regex :

import re   s=""  open('your_file_name','r') myfile:   = myfile.readlines()  in a:  s +=i  list1=list()  list2=list()    list1.append(re.findall('(a = .* ) alpha | (b = .* ) beta | (c = .* ) gamma', s ,re.m))    list2.append(re.findall('(.*) !',s)) in list2:   print i[1]  in list1 :  j in i:   print j[0],j[1],j[2] 

output:

25   300   300      =    2.467585     b =   30.000000     c =   30.000000  




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -