1
$\begingroup$

I have a range of values. Which comes from python-redmine in column one by one. I tried to make a list out of this by wrapping it in [] and thus I get only the first value. This is already a result. I have enough. But I do not understand how to work with it using regular expressions. I need to split the parameter into epxx, seqxx, shxx where xx = two numbers. I wrote a regular expression

ep = r'\s*([ep]+)(\d+)\s*'
string = r[issues]
match = re.search(ep, string)
print(f'{match[1]}{match[2]}')

seq = r'\s*([seq]+)(\d+)\s*'
match = re.search(seq, string)
print(f'{match[1]}{match[2]}')

sh = r'\s*([sh]+)(\d+)\s*'
match = re.search(sh, string)
print(f'{match[1]}{match[2]}')

and checked that it works with the basic version. But if I insert my "list" then everything breaks down

test = redmine.issue.filter(
     project_id='super-rally-s02',
     assigned_to_id='15',
     status_id='2'
 )
 

    for issues in test:
        print(issues)
    
    issues == [issues]

enter image description here this is how the values ​​I got look like and I need a little help to continue working

import re 
ep = r'\s*([ep]+)(\d+)\s*' 
string = r'---   ep02_seq03_sh02   ---' 
match = re.search(ep, string)  
print(f'{match[1]}{match[2]}') 


seq = r'\s*([seq]+)(\d+)\s*' 
match = re.search(seq, string)  
print(f'{match[1]}{match[2]}') 


sh = r'\s*([sh]+)(\d+)\s*'  
match = re.search(sh, string)  
print(f'{match[1]}{match[2]}') 

here's what it looks like for a single file. Now we need to put one value of the issues list there. And to be more precise, create a list of issues

enter image description here

does not find the value despite trying to pass the value from the list all the time none

$\endgroup$
3
  • $\begingroup$ if you need to insert item into the list, use list.append(item) $\endgroup$
    – kemplerart
    Jul 16 at 18:26
  • $\begingroup$ No, what you want to do is list(test). If the test object is an iterable object then that should convert it to a list object. Though I'm not sure what any of this has to do with the regex part of your question, You should consider updating your post with a reproducible example that we could copy into our own text editor to debug. There are a number of issues I can see with the code you've posted outside of that issues alone. $\endgroup$
    – Jakemoyo
    Jul 16 at 18:34
  • $\begingroup$ test is a filter to find values ​​for for issues in test: print(issues) while issues are already filtered data by the required type. What is it for? In the future, I need to use parts of the name as part of the path to the blend file in order to open it $\endgroup$
    – Welet
    Jul 16 at 18:39

1 Answer 1

1
$\begingroup$

So you can do this in couple of ways.

One, try just doing:

list(test)

If test is an iterable object then it might just convert straight to a list.

If that doesn't work you can also try

# declare an empty list
issues = []

#loop over the iterable test object
for issue in test:
    # use the append method of the issues list object to add each issue to it
    issues.append(issue)

You can also do this even simpler with list comprehension.


issues = [issue for issue in test]

But again, if you're able to do the last two then the first one ought to work right away, because the second two require the object to be iterable.

A few other pointers:

print() only writes the value to the standard output. If you want to store the value in something else that won't do it.

Writing [variable] doesn't convert the variable to a list, it just puts that object inside of a list.

Writing variable == other_var with two "==" rather than one is asking Python for the identity of the object, not setting the value. Setting the value is one "=". Using "==" will always return True or False (Boolean).

Like:

a = 1
b = 1
c = 2

print(a == b)
>>> True
print(b == c)
>>> False

EDIT: Regarding your additional question about RegEx:

So if you want to do an operation to a list of items, again you want to do use a for item in list: loop like described above.


for issue in test:
    regex = r'\s*([sh]+)(\d+)\s*'  
    # read the documentation about re.search()
    # the args are (pattern, string)
    # so if you're searching for a regex in each "issue" object then
    # you need to be passing 'issue', not whatever 'string' is.
    match = re.search(regex, issue)
    print(match.group())

Additionally, your regular expression is not correct, which means you will always return None, because it's not finding it, even if you do figure out a loop.

You could honestly do something like:

matches = []
for s in issues:
   ep = re.search('ep\d+', s).group()
   seq = re.search('seq\d+', s).group()
   sh = re.search('sh\d+', s).group()
   matches.append([ep, seq, sh]) 

Or just omit regex altogether and just do:

matches = [issue.split('_') for issue in issues]

Which would give you the same exact result.

$\endgroup$
5
  • $\begingroup$ I added a little to my question. tell me what am I doing wrong? How to correctly pass a value from a list to a regular expression $\endgroup$
    – Welet
    Jul 17 at 8:54
  • $\begingroup$ I added to my answer $\endgroup$
    – Jakemoyo
    Jul 17 at 10:06
  • $\begingroup$ to any method the answer is "expected string or bytes-like object" $\endgroup$
    – Welet
    Jul 17 at 12:32
  • $\begingroup$ location: <unknown location>:-1 Error: Python: File "\Text", line 18 seq = re.search('seq\d+', s).group() ^ IndentationError: unindent does not match any outer indentation level location: <unknown location>:-1 Error: Python script failed, check the message in the system console $\endgroup$
    – Welet
    Jul 17 at 12:39
  • $\begingroup$ The first one means the re.search method needs the s variable to be a string, you aren't passing it a string. The second error means you aren't indenting correctly. I would recommend looking into some basic python tutorials before you dive straight into solving problems like this. You need to have a grasp on the fundamentals first and it's kind of clear you don't have that yet. $\endgroup$
    – Jakemoyo
    Jul 17 at 14:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.