write file

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Jul 5 18:09:09 EDT 2011


On Jul 5, 2011 2:28 PM, "miguel olivares varela" <klica_sk8 at hotmail.com>
wrote:
>
>
> Hi,
>
> i got a csv file that i need to modify and create a new one,  i have no
problem to read mi 'test.cvs' which is the source file but when i try to
create a new one with the modifications i only got the first row in my
'out.csv' file.  I think there is somethng wrong in my loop because i can't
put into the rest.
>
>

>
> [here my code:]
>
> import sys
> import csv
> import os
> import glob
> import time
>
> dir_cdr = "/tmp"
> #loop to find files csv in a directory and read thoses files
> for cdr_files_in in glob.glob(os.path.join(dir_cdr, '*.csv') ):
>         file_source = open(cdr_files_in, 'r')
>         reader = csv.reader(file_source, delimiter=';',
quoting=csv.QUOTE_NONE)
>         try:
>                 for data in reader:
>                         if data:
>                                 firstname = data[0]
>                                 lastname = data[1]
>                                 date_source = data[2]
>                                 phone = data[3]
>                                 #Date to epoch
>
timestamp=int(time.mktime(time.strptime(date_cdr, "%Y%m%d %H%M%S")))
>                                 fout = open("out.csv", "w")
>                                 print >>fout, lastname, firstname,
timestamp, phone

Do you understand what these next two lines are doing? They are closing the
file and then exiting the program. I'm pretty sure that's not what you want
to do here.

>                                 fout.close()
>                                 sys.exit()
>                 file_source.close()
>         except csv.Error, e:
>                 print e
>                 file_source.close()
>                 sys.exit('file %s, line %d: %s' % (file_source,
reader.line_num, e)
>
> [out.csv]
> Smith John 1296208720 33611111111
>
>
> Could you help me?
>
> Best Regards,
> Miguel
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110705/b819fbf6/attachment-0001.html>


More information about the Python-list mailing list