简体   繁体   中英

How to obtain multiple lines from a input file when the file sometimes contains random empty lines in python

There are a lot of questions about reading input files out there, but none of the once I've seen have helped me. It's probably easier to understand if I show you a part of the input file first. The input file is created by a program, so there is nothing I can do 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 from 4 more force components: FXZ, MXX, MXY and MXz here>

I put in there just an indication. There is an actual square box (not just a whitespace) in the input file, but it couldn't get it to show in this message.

I am trying to get first column (starting with 35.0) and the second column from the right on both FXX and FXY into separate lists, ie Fxx = [.,.,.,.] and Fxy = [.,.,.,.,.]. Then i want to write then to a new file.

I know how many element there should be in a column (6 in this case), so what I've been trying is search for 'SECL' and then write that line plus the 5 next ones into the file. This works for FXX, but not FXY. post_inpfile is the name of the inputfile you can see further up. post_inppath is the path to the same file.

# <irrelevant code here>
frequency = []
force_count = 0
Fxx = []
Fxy = []
num_freq = 6

for linenum,line in enumerate(post_inpfile, 1):
  if len(line.split())==0: # Check if line is empty, and if so skip it. Also catches square boxes
    pass
  else:
    try:
      if line.split()[0][0:4] == 'SECL':
        force_count+=1
        for i 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 for 4 more force components>  
        freq_count+=1 # Makes sure frequency list is only created once               
   except:
     pass

# <more irrelevant code>

I've used insert instead of append on Fxx, Fxy etc since they should be sorted from low to high frequency.

This produces a Fxx list with 6 elements, but a Fxy list with 5 elements. All should be 6. I've concidered dropping the 'for' loop (for i in range(num_freq)) into a while loop for each component to see if the length is 6. But, i'm not really sure how to do this while only reading the file once, besides it seems like this solution produces a lot of unnecessary code. I would to do it more generally and elegantly.

It should be mentioned that the lists could contain any number of elements, not just 6. And those square boxes are placed randomly.

If there is some information missing in this post, please let me know. Your help is much appreciated. This is not school work.

Add something like this

import re
check=re.compile("\d")
def hasnumbers(a):
    return check.search(a)

Then hasnumbers(line) will return true for good lines and false for blank and "box" lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM