I have a text file like the following:
#some_line
@another_line
original_string1|new_string1
#some_other_line
@and_another_line
original_string2|new_string2
I want to be able to associate every line with an @ to the preceding line with a #. I cannot seem to figure out a strategy to achieve this in python.
Here is my current code:
with open(self.file, 'r') as f:
for i, line in enumerate(f):
line = line.strip(' \t\n\r')
if '#' in line[:1]:
self.parent[i] = line[1:]
if '@' in line[:1]:
self.child[i] = line[1:]
if '|' in line:
key, value = line.split('|')
self.strings[key] = value
I need to be able to reference each parent entry and associate the child entries with it. The lines with a '|' also need to be associated with the parent as well.
'#' in line[:1]is very odd. Why not justline[0] == '#'or, even better,line.startswith('#')? Are you trying to check something more complicated than that?@line for each#line? If so, your code should buildparentandchildlists that match things up properly. Is that not working? Or do you just not know how to build a mapping out of two parallel lists?