Aug-08-2022, 02:34 AM
(This post was last modified: Aug-08-2022, 03:03 AM by deanhystad.)
The file needs to look like the example posted by oldtrafford.
Output:Text
..
..
...
..
..
*Nset, nset=PATH, unsorted
13, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735
...
2559, 2560, 2561, 2562, 2563, 2564, 72
*Text
....
...
...
..
Text
I wouldn't be surprised if the starting mark should be Nset, not *Nset, and the ending mark Text, not *Text. Looking at it again I think maybe the numbers start after the Nset line and continue until there is a line that starts with text. Maybe this is a better fit:import json import re integer_pattern = re.compile("[+-]?[0-9]+") def get_numberes_from_file(filename): numbers = [] with open(filename, "r") as file: for line in file: # Marks the beginning of int data if line.startswith("Nset"): break for line in file: # Read lines until encounter line without numbers matches = map(int, re.findall(integer_pattern, line)) if matches: numbers += matches else: break return numbers input_files = ["test.txt", "test2.txt", "test3.txt"] numbers = {} for filename in input_files: numbers[filename] = get_numberes_from_file(filename) with open("output.inp", "w") as file: json.dump(numbers, file, indent=4)