How to obtain multiple lines from a input file when the file sometimes contains random empty lines in python -
there lot of questions reading input files out there, none of once i've seen have helped me. it's easier understand if show part of input file first. input file created program, there nothing can there.
seclfxx 150.00 0.000 35.000 3.213e+03 -7.624e+03 8.274e+03 -67.151 17.000 -3.549e+04 1.012e+04 3.690e+04 164.084 16.000 -4.755e+04 -5.719e+03 4.789e+04 -173.141 15.500 -4.591e+04 -2.862e+04 5.410e+04 -148.062 15.000 -2.781e+04 -7.743e+04 8.227e+04 -109.756 14.500 2.492e+04 -1.973e+05 1.988e+05 -82.799 seclfxy 150.00 0.000 35.000 3.213e+03 -7.624e+03 8.274e+03 -67.151 17.000 -3.549e+04 1.012e+04 3.690e+04 164.084 16.000 -4.755e+04 -5.719e+03 4.789e+04 -173.141 15.500 -4.591e+04 -2.862e+04 5.410e+04 -148.062 <square box> 15.000 -2.781e+04 -7.743e+04 8.227e+04 -109.756 14.500 2.492e+04 -1.973e+05 1.988e+05 -82.799 <data 4 more force components: fxz, mxx, mxy , mxz here>
i put in there indication. there actual square box (not whitespace) in input file, couldn't show in message.
i trying first column (starting 35.0) , second column right on both fxx , fxy separate lists, i.e fxx = [.,.,.,.] , fxy = [.,.,.,.,.]. want write new file.
i know how many element there should in column (6 in case), i've been trying search 'secl' , write line plus 5 next ones file. works fxx, not fxy. post_inpfile name of inputfile can see further up. post_inppath path same file.
# <irrelevant code here> frequency = [] force_count = 0 fxx = [] fxy = [] num_freq = 6 linenum,line in enumerate(post_inpfile, 1): if len(line.split())==0: # check if line empty, , if skip it. catches square boxes pass else: try: if line.split()[0][0:4] == 'secl': force_count+=1 in range(num_freq): freq = linecache.getline(post_inppath, linenum+i) if freq_count==0: try: frequency.insert(0,freq.split()[-5]) except: pass if force_count==1: try: fxx.insert(0,freq.split()[-2]) except: pass elif force_count==2: try: fxy.insert(0,freq.split()[-2]) except: pass <same check 4 more force components> freq_count+=1 # makes sure frequency list created once except: pass # <more irrelevant code>
i've used insert instead of append on fxx, fxy etc since should sorted low high frequency.
this produces fxx list 6 elements, fxy list 5 elements. should 6. i've concidered dropping 'for' loop (for in range(num_freq)) while loop each component see if length 6. but, i'm not sure how while reading file once, besides seems solution produces lot of unnecessary code. more , elegantly.
it should mentioned lists contain number of elements, not 6. , square boxes placed randomly.
if there information missing in post, please let me know. appreciated. not school work.
add this
import re check=re.compile("\d") def hasnumbers(a): return check.search(a)
then hasnumbers(line)
return true lines , false blank , "box" lines
Comments
Post a Comment