I am new to Python and I am trying to parse a file. Only some lines in the file contain data of interest, and I want to finish using a dictionary of material analyzed from valid matching lines in the file.
The code below works, but it's a little ugly, and I'm trying to find out how this should be done, perhaps with understanding, or with multi-line regex. I am using Python 3.2.
file_data = open('x:\\path\\to\\file','r').readlines()
my_list = []
for line in file_data:
if re.search(pattern, line):
one_tuple = re.search(pattern, line).group(3,2)
my_list.append(one_tuple)
my_dict = dict(my_list)
Can you suggest a better implementation?
source
share