I have text file with this kind of data:
height 10.3
weight 221.0
speed 84.0
height 4.2
height 10.1
speed 1.2
I want to read the file and each time I find one of the keywords height
, weight
, or speed
I want to call a different function. For example, if I encounter the height
keyword want to call the function convert_hight(h)
.
The keywords may appear in any order throughout the file, but they always appear at the beginning of the line.
I must point out that this is a simplified example, in reality I have hundreds of keywords and the text file may be quite large, so I want to avoid comparing each word in the file with each word in the keyword list.
How can I approach this problem? (I'm using python)
You can use a dictionary of functions: