I am reading a text file with many thousands of records (one per line). Each starts with 5 characters, first is alpha followed by 4 numeric e.g. H0100.
The list of available starting labels is defined so not random although the last numeric value could be anything from 1-9 (sometimes last two could be 1-9).
so a class of record could be any of the following H0100, H0101.....H0109
There are instances when the last to numbers have a range such that there would be 100 combinations.
I would like to create a function to process each record such that if the first 3 or 4 characters in the label are identified the same function is called. The reason being is the records are essentially identical in format and can be processed the same even though the last one or two values in the label changes.
Here is how I am doing it at the moment for the case H0100, H0101.....H0109
for line in (file):
#turn the first 5 characters into an object
obj = eval (line[0:5])
obj (line)
def H0100(line)
.....process line
#Just point all the alternative to the one function
H0101 = H0102 = H0103 = H0104 = H0105 = H0106 = H0107 = H0108 = H0109 = H0100
This works but it does not look very elegant and I am sure it is not Pythonic! Anyone have any suggestions as to how I could make this simpler/more elegant? I have 40-50 labels with one character change and few with two character changes as such have to point 100 alternatives to one function.
Many thank.