小能豆

Splitting a line(Fixed-size chunks) and identifying adjacent word in python

java

Would like to split below line

*SECTION_TSHELL
$#   secid    elform      shrf       nip     propt   qr/irid     icomp    tshear
         1         2       0.0         5         0       0.0
*MAT_ELASTIC
$#     mid        ro         e        pr        da        db  not used
     Steel  8000.0002.0000e+11  0.300000       0.0       0.0       0.0

The format of the file is such that every pid, secid etc. takes 10 spaces again & again. Example in above

Step 1: The Bold letters takes 10 ($# mid) spaces when you select. with this would like to split a line using specific number of spaces. Step 2: After splitting, the adjacent value need to be identified.

import os

ModelPath = 'D:\pyLearning\Read_all_files'

k_files = os.listdir(ModelPath)

if k_files:

    include_file = "_K3_Controls.k"

    include_file_path = os.path.join(ModelPath, include_file)

    with open(include_file_path, 'r') as include_kfile:
        parts = include_kfile.read()
        print("TestEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEnd")

        for lines in parts:
            if not lines.startswith('$#'):
                # parts = lines.split()
                length = 10
                splits = [parts[i:i + length] for i in range(0, len(parts), length)]
                # print("Worked", splits)
                try:
                    i = splits.index("Steel")
                    next_word = splits[i + 1]
                    print(f"Steel = {next_word}")
                except(ValueError, IndexError):
                    pass

print(“finished”)
Expecting the output somewhat like this Worked [‘Steel’, ‘8000.000’, ‘2.0000e+11’, ‘ 0.300000’]

and at the end

Steel = 8000.000

Thanks in advance


阅读 95

收藏
2023-11-23

共1个答案

小能豆

It looks like there are a couple of issues in your code. I’ll help you address them. First, you need to properly read lines from the file. Second, when splitting the lines, you should use spaces as delimiters. Finally, to find the index of ‘Steel’, you need to look for it in the list of splits, not in the original string.

Here’s an updated version of your code:

import os

ModelPath = 'D:\pyLearning\Read_all_files'

k_files = os.listdir(ModelPath)

if k_files:

    include_file = "_K3_Controls.k"

    include_file_path = os.path.join(ModelPath, include_file)

    with open(include_file_path, 'r') as include_kfile:
        for line in include_kfile:
            if not line.startswith('$#'):
                # Use spaces as delimiters to split the line
                parts = line.split()

                # Find the index of 'Steel' in the list of splits
                try:
                    i = parts.index("Steel")

                    # Get the next word (index + 1)
                    next_word = parts[i + 1]

                    # Print the result
                    print(f"Steel = {next_word}")
                except (ValueError, IndexError):
                    pass

print("finished")

This code reads each line from the file, splits it into parts using spaces as delimiters, and then looks for the index of ‘Steel’ in the list of parts. If found, it prints the next word (the value associated with ‘Steel’). Make sure to adjust the file path and other details as needed for your specific case.

2023-11-23