[Tutor] converting xls to csv
Nick Burgess
burgess.nick at gmail.com
Sat Jun 6 21:19:11 CEST 2009
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? 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:
for row in f:
for cell in row:
if pattern.search(cell):
print ', '.join(row)
Traceback (most recent call last):
File "Q:\FILES\COMPUTER_NETWORKING\Python\CVSs\ipSrch-v1.0.py", l
csv.reader(open (files), delimiter=' ', quotechar='|')
TypeError: coercing to Unicode: need string or buffer, list found
On Sun, May 31, 2009 at 12:45 PM, Richard
Lovely<roadierich at googlemail.com> wrote:
> 2009/5/31 Nick Burgess <burgess.nick at gmail.com>:
>> Got it.
>>
>> the row is not a string or buffer but the cell is..
>>
>> for row in spamReader:
>> for cell in row:
>> if pattern.search(cell):
>> print ', '.join(row)
>>
>>
> Alternatively, if you know that the string you want to search for only
> appears in a single cell from the row, you could use
>
> for row in spamReader:
> if pattern.search(cell[coll_number_here]):
> print ', '.join(row)
>
> If there is a range of adjacent cells that could contain the text, you
> could try something like
> if pattern.search('|'.join(row[3:5])
>
> For non-adjacent cells, try something like
>
> targetCells = [1,3,5]
> for row in spamReader:
> if pattern.search('|'.join(row[i] for i in targetCells))
>
> Both of these solutions will be faster than iterating over th entire
> row, unless tht is specifically what you want.
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
>
More information about the Tutor
mailing list