Thank you Andres, I really like this script. I am using it to extract the SDSS catalog magnitudes for my images with Sloan g'r'i' filters. It is saving me a lot of time.
Because the .csv is semi-colon separated, opening in Excel does not auto populate the columns. One has to either manually copy paste or convert the semi-colon to comma and then open.
So it would be great if the output .csv has comma instead of the semi-colon separator - or perhaps an option for the user to use a separator.