First, you cannot write a “universal” or “smart” transformation that magically handles everything.
Secondly, trying to summarize the conversion of strings to data into anything other than code does not seem to work well. So instead of writing a line that calls the conversion, just write the conversion.
Finally, trying to write a configuration file in a domain-specific language is stupid. Just write Python code. This is not much more complicated than trying to parse any configuration file.
Is it possible or do I need to do some other things?
Do not waste time trying to create a "type file" that is not just Python. It does not help. It’s easier to write a transformation as a function of Python. You can import this function as if it were your "type file".
import datetime def convert( row ): return dict( id= int(row['id']), value= str(row['value']), date= datetime.datetime.strptime(row['date],"%Y-%m-%d %H:%M:%S"), )
That's all you have in your "type file"
Now you can read (and process) your input this way.
from type_file import convert import csv with open( "date", "rb" ) as source: rdr= csv.DictReader( source ) for row in rdr: useful_row= convert( row )
in many cases I don't know the number of columns or data type until runtime
This means that you are doomed.
You must have an actual definition of the contents of the file or you cannot perform any processing.
"id","value","other value" 1,23507,3
You don’t know if "23507" should be an integer, a string, a zip code or a floating point (which missed the period), duration (in days or seconds) or any other more complicated thing, you cannot hope, and you don’t you can guess.
After receiving the definition, you need to write an explicit conversion function based on the actual definition.
After recording the conversion, you need to (a) test the conversion with a simple unit test and (b) check the data to make sure that it is actually being converted.
Then you can process the file.