Aug-07-2022, 11:24 PM
(This post was last modified: Aug-07-2022, 11:25 PM by Pedroski55.)
@deanhystad I often copy stuff from you experts here and try it out at home. It's a good way to learn.
I made a text file with some text. Added some numbers on each line, then copied all the numbers from above in as well.
But when I try your code, it returns an empty list. Obviously, I'm doing something wrong, but I can't see what. Could you help?
I made a text file with some text. Added some numbers on each line, then copied all the numbers from above in as well.
But when I try your code, it returns an empty list. Obviously, I'm doing something wrong, but I can't see what. Could you help?
Quote:>>> for filename in input_files:
numbers[filename] = get_numberes_from_file(filename)
[]
import json import re path2text = '/home/pedro/temp/' myfile = 'test_number_finder.txt' integer_pattern = re.compile("[+-]?[0-9]+") def get_numberes_from_file(filename): numbers = [] with open(path2text + 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_number_finder.txt'] numbers = {} for filename in input_files: numbers[filename] = get_numberes_from_file(filename)