Sunday, June 2, 2013

Ignore bracket in csv file python

Ignore bracket in csv file python

I wrote a python script for a friend that:
takes a CSV of photos she's been cataloging that has the name of the photos in an ordered list
finds the image files on the filesystem
matches the files in the csv with files on the system
copies the images on the filesystem to a folder with a figure name in the order the files appear in the CSV
So essentially, it does:



INPUT: myphoto1.tiff, mypainting.jpeg, myphoto9.jpg, orderedlist.csv
OUTPUT: fig001.jpg, fig002.tiff, fig003.jpeg



This works fine except we ran into an issue where some of the files (all by the same photographer) have 1 bracket in them, e.g.
myphoto[fromitaly.jpg
This seems to break my regular expression search:
The relevant code:
orderedpaths = [path for item in target for path in filenames if re.search(item, path)]
Where filenames is a list of the photo files on the system and target is the list from the CSV. This code is supposed to match the CSV file name (and it's subsequent order in the list) to the filename to give an ordered list of the filenames on the system.
The error:
Traceback (most recent call last):
  File "renameimages.py", line 43, in <module>
    orderedpaths = [path for item in target for path in filenames if re.search(item, path)]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 142, in search
    return _compile(pattern, flags).search(string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 244, in _compile
    raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression
I tried or considered:
Changing the filenames/csv, but this isn't scalable and ideally her department will be using this script more in the future
Investigating treating the files as "raw" -- but it didn't seem like that was possible for input from CSV
Deleting the [ character from the input, but the problem is that then the input won't match the actual files on the system.
I suppose I should mention I only suspect this was the issue: by printing out the progress of the code, it appears as if the code gets to the CSV item with the bracket and errors.