[Tutor] Convert values in a list back and forth from ints and time
alan.gauld at btinternet.com
Tue Jan 6 09:46:16 CET 2009
"Sander Sweers" <sander.sweers at gmail.com> wrote
> I use the csv module to read and write a csv file. When I read the
> file into a new list I convert the ints and the dates to int and
> When writing the lines back to a csv file I loop over the lists and
> convert the values back with the same function but in reverse
> I was just wondering if there is another way of dealing with this.
If you always convert the values back to strings then you could
just hold onto the original strings by storing them in a (str, val)
If you do calculations and modify anmy of them then convert the
value/string there and then and modify the tuple. You could even
make it a class with the convertion fiunctions being methods.
But whether the extra complexity is worthwhile will depend on
what proportion of values get modified. If its a small proportion
then the gain in speed from not doing the reverse convertion might
be worth it. In most cases what you have done is probably the
simplest approach and if its fast enough then don't worry about it!
PS. I agree with Bob about splitting the function in two...
Author of the Learn to Program web site
More information about the Tutor