On 7 Feb 2007 11:31:32 -0800, James <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm a newbie to Python & wondering someone can help me with this...
>
> I have this code:
> --------------------------

> #! /usr/bin/python
>
> import sys
>
> month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':
> 8,'SEP':9,'OCT':10,'NOV':11,'DEC':12}
> infile=file('TVA-0316','r')
> outfile=file('tmp.out','w')
>
> for line in infile:
>     item = line.split(',')
>     dob = item[6].split('/')
>     dob = dob[2]+'-'+str(month[dob[1]])+'-'+dob[0]
>     lbdt = item[8].split('/')
>     lbdt = lbdt[2]+'-'+str(month[lbdt[1]])+'-'+lbdt[0]
>     lbrc = item[10].split('/')
>     lbrc = lbrc[2]+'-'+str(month[lbrc[1]])+'-'+lbrc[0]
>     lbrp = item[14].split('/')
>     lbrp = lbrp[2]+'-'+str(month[lbrp[1]])+'-'+lbrp[0]
>     item[6] = dob
>     item[8] = lbdt
>     item[10]=lbrc
>     item[14]=lbrp
>     list = ','.join(item)
>     outfile.writelines(list)
> infile.close
> outfile.close
> -----------------------------
>
> And the data file(TVA-0316) looks like this:
> -----------------------------
> 06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
> NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,,
> 06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
> NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h,
> 06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
> NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h,
> 06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
> NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,,
> -----------------------------
>
> Basically I'm reading in each line and converting all date fields (05/
> MAR/1950) to different format (1950-03-05) in order to load into MySQL
> table.
>
> I have two issues:
> 1. the outfile doesn't complete with no error message.  when I check
> the last line in the python interpreter, it has read and processed the
> last line, but the output file stopped before.
> 2. Is this the best way to do this in Python?
> 3. (Out of scope) is there a way to load this CSV file directly into
> MySQL data field without converting the format?
>
> Thank you.
>
> James
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Your script worked for me. I'm not sure what the next step is in
troubleshooting it. Is it possible that your whitespace isn't quite
right? I had to reformat it, but I assume it was because of the way
cut & paste worked from Gmail.

I usually use Perl for data stuff like this, but I don't see why
Python wouldn't be a great solution. However, I would re-write it
using regexes, to seek and replace sections that are formatted like a
date, rather than breaking it into a variable for each field, changing
each date individually, then putting them back together.

As for how MySQL likes having dates formatted in CSV input: I can't
help there, but I'm sure someone else can.

I'm pretty new to Python myself, but if you'd like help with a
Perl/regex solution, I'm up for it. For that matter, whipping up a
Python/regex solution would probably be good for me. Let me know.

Shawn
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to