Aug-07-2022, 09:45 PM
(This post was last modified: Aug-07-2022, 09:45 PM by deanhystad.)
I don't understand what you mean by "save the dictionary in a text file"? What file format do you want to use? What are the keys in the dictionary?
This code saves the dictionary as a json format file. For keys I use the filename of the input file.
This code saves the dictionary as a json format file. For keys I use the filename of the input file.
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: if line.startswith("*Nset"): break for line in file: if line.startswith("*Text"): break numbers += map(int, re.findall(integer_pattern, line)) print(numbers) 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)I don't understand what you were doing with the short name output files.