Issue
I do some (beginners) programming in ipython 5.1.0.
There are about 100 directories with names like
Mix_XFUEL_0.5_XOXID_0.6_PHI_0.2
, only the numbers are changing.
I use fnmatch
to read the names into the programm.
Now I need all directories, in which the first two numbers (behind XFUEL
and XOXID
) are identically, like:
Mix_XFUEL_0.5_XOXID_0.5_PHI_0.2
Mix_XFUEL_0.5_XOXID_0.5_PHI_0.4
Mix_XFUEL_0.5_XOXID_0.5_PHI_0.6
but also
Mix_XFUEL_0.6_XOXID_0.6_PHI_0.2
Mix_XFUEL_0.6_XOXID_0.6_PHI_0.4
Mix_XFUEL_0.6_XOXID_0.6_PHI_0.6
I tried:
i = '0.5'
fnmatch.fnmatch(file, 'Mix_XFUEL_'i'_XOXID_'i'_PHI_*'):
but it won't work.
How can it be done?
Solution
use regex, 2nd capture group (\1)
asserted that it need to match the 1st capture group (\d+(?:\.\d+)?)
(\d+(?:\.\d+)?)
match any decimal number or number with decimal points
import re
re.match(r'Mix_XFUEL_(\d+(?:\.\d+)?)_XOXID_(\1)_PHI', infile)
this will match
- "Mix_XFUEL_0.5_XOXID_0.5_PHI_0.2"
- "Mix_XFUEL_0.6_XOXID_0.6_PHI_0.2"
- "Mix_XFUEL_5_XOXID_5_PHI_0.2"
but not
- "Mix_XFUEL_0.5_XOXID_0.6_PHI_0.2"
Answered By - Skycc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.