-2

I want to read and print lines of numbers between two spesific lines. So my reading should start under Face9 and end ***********************************************************. I should use stars(*) as parameters when I try to find and read this numbers. This data is in inp file. So I should use python file reading functions.

I tried to use find and seek functions. I found a position of Face9 with find function but When I try to use it with seek function and read after this position, I can't reach numbers that I want. In short, find and seek position doesn't matched.

Data that I want to read and print between stars(*)

***********************************************************
** constraints force node loads
*CLOAD
** ConstraintForce
** node loads on shape: Connect:Face9
10,1,0.0000000000000E+00
11,1,0.0000000000000E+00
26,1,0.0000000000000E+00
27,1,0.0000000000000E+00
80,1,6.2500000000000E+01
152,1,0.0000000000000E+00
153,1,0.0000000000000E+00
154,1,0.0000000000000E+00
155,1,6.2500000000000E+01
156,1,6.2500000000000E+01
157,1,6.2500000000000E+01
158,1,6.2500000000000E+01
159,1,0.0000000000000E+00
160,1,0.0000000000000E+00
161,1,0.0000000000000E+00
162,1,6.2500000000000E+01
163,1,6.2500000000000E+01
164,1,6.2500000000000E+01
165,1,6.2500000000000E+01
166,1,6.2500000000000E+01
424,1,1.2500000000000E+02
425,1,1.2500000000000E+02
426,1,1.2500000000000E+02
427,1,1.2500000000000E+02
428,1,1.2500000000000E+02
429,1,1.2500000000000E+02
430,1,1.2500000000000E+02



***********************************************************
  • No images of text please. Post your text as [formatted text](/help/formatting) instead of a screenshot. Also, please show your code as a [mre] instead of describing it, and give more details on why that code fails to meet your expectations – Pranav Hosangadi Dec 06 '22 at 22:28
  • Please do not include images to convey textual information in your question. Instead, copy and paste the information directly into your question. Generally speaking, you will have better luck if you read in the entire file and then process it in memory. You can read it in as a list of lines, or read it as a single binary array that you can convert to a string. Either way, seeking around in the file is usually not the best way to go at such a problem. The exception would be if you have a gigantic file that won't fit in memory. – CryptoFool Dec 06 '22 at 22:28
  • @CryptoFool processing the file line-by-line allows you to read in only the part of the file you care about, processing a file like this line-by-line would be much more efficient than reading in the entire file and _then_ iterating over lines. You can't seek around text files anyway... – Pranav Hosangadi Dec 06 '22 at 22:30
  • @PranavHosangadi - In the majority of cases like this, it is much more efficient to read in an entire file and operate on it in memory. Seeking in a small file is very inefficient, as it invalidates the read buffer. A small file like this is likely read into memory in its entirety no matter what the first read operation is. After that, seeks in a small file may cause the whole file to be read again. For a file like the one shown by the OP, there's nothing to be gained by reading the file line by line, and the logic of processing the file in memory is usually more straightforward. – CryptoFool Dec 06 '22 at 22:48

2 Answers2

0

Maybe something like that will work:

with open('your_filepath', 'r') as f:
    all_lines = f.readlines()
    nums_lst = [line for line in all_lines if '*' not in line]
    print(nums_lst)
nokla
  • 451
  • 1
  • 8
0

If you just want to ignore lines that start with '*', the easiest thing to do would be to just check the first character of each line and not process the line if it starts with '*'. If you really care about finding Face9 specifically at the end of a line, you could do something like this:

filepath = '/tmp/data.txt'

def process_line(line):
    parts = line.split(',')
    if len(parts) != 3:
        raise Exception("Unexpected data format")
    print(*parts)

def main():
    with open(filepath) as f:
        found_face9 = False
        for line in f:
            line = line.strip()
            if line == '':                # skip blank lines
                continue
            if found_face9:
                if line.startswith('*'):  # stop reading when we we see a line that starts with '*'
                    break
                process_line(line)
            elif line.endswith('Face9'):
                found_face9 = True.       # found the end of the header, so start processing

main()

Result:

10 1 0.0000000000000E+00
11 1 0.0000000000000E+00
...
429 1 1.2500000000000E+02
430 1 1.2500000000000E+02

Note that this does read the file line by line in opposition to what I said in my comment. Doing so is fine as long as you don't do any seeking. If you have a large file, reading line by line does limit memory requirements, but also takes advantage of buffering to batch reading into large chunks.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44