I've got a problem trying to replace keys (dictionary) that I have in a file, with its corresponding values. More details: an input file called event_from_picks looks like:
EVENT2593
EVENT2594
EVENT2595
EVENT41025
EVENT2646
EVENT2649
Also, my dictionary, created by reloc_event_coords_dic() looks like:
{'EVENT23595': , 'EVENT2594': , 'EVENT2595': , 'EVENT41025': , 'EVENT2646': , 'EVENT2649': }
What I'd like to end up with, is a new file with the values instead of the keys. In this case, a new file called receiver.in which will look like:
36.9828 -34.0538 138.1554
41.2669 -33.0179 139.2269
4.7500 -32.7926 138.1523
16.2453 -32.9552 138.2604
5.5949 -32.4923 138.1866
7.9533 -31.8304 138.6966
My wrong function (I know I must have a problem with loops but I can't figure out what) so far is:
def converted_lines ():
file_out = open ('receiver.in', 'w')
converted_lines = []
event_dict = reloc_event_coords_dic()
data_line = event_dict.items() # Takes data as('EVENT31933', ['10.1230 -32.8294 138.1718'])
for element in data_line:
for item in element:
event_number = element[0] # Gets event number
coord_line = event_dict.get (event_number, None)
with open ('event_from_picks', 'r') as file_in:
for line in file_in:
if line.startswith(" "):
continue
if event_number:
converted_lines.append ("%s" % coord_line)
file_out.writelines(converted_lines)
Thanks for reading!