I just want to check if there is any better way of doing this rather than using what i came up with.
The thing is that i need to parse a .py file, more precisely i have to look for a specific list named id_list that contains several int numbers. Numbers can be written in several formats.
For example:
id_list = [123456, 789123, 456789]
id_list = [ 123456,
789123,
456789 ]
id_list = [ 123456
,789123
,456789 ]
What i came up with works just fine, but for the sake of perfectionism i want to know if there is "smoother" way of doing so.
with open(filepath, 'rb') as input_file:
parsed_string = ''
start_flag = False
start_parsing = False
for line in input_file:
if 'id_list' in line:
id_detected = True
if id_detected:
for char in line:
if char == '[':
start_parsing = True
if start_parsing and char != '\n':
parsed_string += char
if char == ']':
id_detected = False
start_parsing = False
break
After that has been done im just filtering parsed_string:
new_string = "".join(filter(lambda char: char.isdigit() or char == ',', parsed_string))
Which gets me string containing numbers and commas: 123456,789123,456789
So to wrap this up, is there anything that i could improve?
id_listdirectly?id_list = list()? Orx = []thenid_list = x?