oldDoctor oldDoctor - 4 months ago 9
Python Question

How to parse a list in python using re

I am trying to parse a list to get the individual images returned by the bash script (stored in the list errors in py script). How can I do this with

re
?

bash script

#!/bin/bash
value(){
for entry in *
do
if expr "$(file -b $entry)" : 'JPEG ' >/dev/null;
then
echo -e "$entry"
fi
done
}
value


python code

import subprocess
errors = [subprocess.check_output(['/black.sh'])]
print errors


Output

11_37_24.jpeg
11_38_02.jpeg
11_39_56.jpeg
11_40_20.jpeg
11_40_32.jpeg
11_45_03.jpeg


The list
errors
is getting assigned a string of length 1.

errors = ["11_37_24.jpeg 11_38_02.jpeg 11_39_56.jpeg 11_40_20.jpeg 11_40_32.jpeg 11_45_03.jpeg"]


However I want to get those images separetely to use it in an html page. How can I do that with
re
? Is there any other alternative?

Answer

I make a few assumptions (based on the example input you give):

  • file names only contain digits and underscores (so I can use \w in regex)
  • words in a file name are always concatenated by underscores (no spaces)
  • every file is a jpeg file

The code:

import re

errors = ["11_37_24.jpeg 11_38_02.jpeg 11_39_56.jpeg 11_40_20.jpeg 11_40_32.jpeg 11_45_03.jpeg"]

re.findall('\w+\.jpeg', errors[0])
['11_37_24.jpeg', '11_38_02.jpeg', '11_39_56.jpeg', '11_40_20.jpeg', '11_40_32.jpeg', '11_45_03.jpeg']

If my assumptions are wrong for your project, re is probably not the way to go.

EDIT

The OP used the following code (which was given in the comments):

errors[0].split('\n')