[Tutor] converting xls to csv

Nick Burgess burgess.nick at gmail.com
Sat Jun 6 23:37:52 CEST 2009


Thanks everyone, the following code works great.  It returns the name
of the file and the row that matched the reqex.  Is it posible to take
the regex compile from user input?  To make it take an argument,  like

> csvSearch.py 10.192.55

af = re.compile(sys.argv[1])
pattern = re.compile(af)          ....?



pattern = re.compile(r'10\.191\.2')
files = glob.glob("*.csv")

for f in files:
    ff = csv.reader(open (f, 'rb'), delimiter=' ', quotechar='|')
    for row in f:
        for row in ff:
            for cell in row:
                if pattern.search(cell):
                    print f
                    print ', '.join(row)

On Sat, Jun 6, 2009 at 4:39 PM, Sander Sweers<sander.sweers at gmail.com> wrote:
> 2009/6/6 Emile van Sebille <emile at fenx.com>:
>>> 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='|')
>
> <snip>
>
>>    reader(...)
>>        csv_reader = reader(iterable [, dialect='excel']
>>                                [optional keyword args])
>>            for row in csv_reader:
>>                process(row)
>
> If you are on windows you should open the file in binary mode.
>  open(f, 'rb')
>
> Greets
> Sander
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list