[Tutor] converting xls to csv

Emile van Sebille emile at fenx.com
Sat Jun 6 22:19:45 CEST 2009


On 6/6/2009 1:07 PM Nick Burgess said...
> f seems to be a string containing the file names, so csv.reader isnt
> actually opening them..  maybe i will play with os.walk..?
> 
> files = glob.glob("*.csv")
> 

Almost there...

> for f in files:
>     print f
>     csv.reader(open (f), delimiter=' ', quotechar='|')

you open it here, but don't save a reference to the opened file.  Try...
       ff = csv.reader(open (f), delimiter=' ', quotechar='|')

>     for row in f:

then here, try:
       for row in ff:

>         for cell in row:
>             if pattern.search(cell):
>                 print ', '.join(row)
> 
> 
> XLS.xls.org1.csv
> XLS.xls.org2.csv
> XLS.xls.org3.csv

 From the interactive interpreter, import csv, then you can ask

help(csv)

snipped from that you find...

     reader(...)
         csv_reader = reader(iterable [, dialect='excel']
                                 [optional keyword args])
             for row in csv_reader:
                 process(row)


HTH,


Emile



> 
> On Sat, Jun 6, 2009 at 3:33 PM, Emile van Sebille<emile at fenx.com> wrote:
>> On 6/6/2009 12:19 PM Nick Burgess said...
>>> Thank you. The data is pretty much random throughout the csv's so I
>>> think I it would have to iterate over the entire rows .  I need to
>>> search all .csv files in the current directory.   I can get glob to
>>> return a list of files.  How could I get csv.reader to open all the
>>> files in the list?
>> Open the files from within your loop.
>>
>>> My loop logic must be bad..  I am using
>>> ActivePython 2.6.1.1.  Any clues would be appreciated.
>>>
>>>
>>>
>>> pattern = re.compile(r'10\.191\.239\.0')
>>> files = glob.glob("*.csv")
>>>
>>> csv.reader(open (files), delimiter=' ', quotechar='|')
>>> for f in files:
>> do the open here
>>
>>>    for row in f:
>>>        for cell in row:
>>>            if pattern.search(cell):
>>>                print ', '.join(row)
>>>
>>
>> Emile
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list