-2

I'm trying to extract from this list

[<JIRA Issue: key='HDDIS-42214', id='855344'>,
 <JIRA Issue: key='HDDIS-42171', id='854930'>,
 <JIRA Issue: key='HDDIS-42170', id='854929'>]

the 3 strings:

HDDIS-42214
HDDIS-42171
HDDIS-42170

How can do this?

PS: At some point it can be 3, another 1, another 2...

Timus
  • 10,974
  • 5
  • 14
  • 28
Orion
  • 9
  • 4
  • Can you show your code? – Ajay Mar 31 '22 at 10:39
  • Yes, i have tried with strip, rstrip and lstrip but it gets the other part of the text and i need get only the fields inside key. – Orion Mar 31 '22 at 10:55
  • What you have doesn't look like text, it looks like a list. Please provide a [mre]. – martineau Mar 31 '22 at 11:01
  • Yes, sorry, i't s a list. If a reproduce the example of @ratnesh and i do print (res) it gives me an error: TypeError: expected string or bytes-like object – Orion Mar 31 '22 at 11:13
  • 1
    You need to provide more information about what the list exactly contains (what is the `type` of the items). My guess would be [`repr` strings of `Issue` objects](https://github.com/pycontribs/jira/blob/main/jira/resources.py#L189) - but that's just a guess. – Timus Mar 31 '22 at 13:56
  • 1
    If the list contains `repr` strings, then it's missing quote characters (i.e. it's not a valid Python represent of a list and its contents). – martineau Apr 01 '22 at 11:00

1 Answers1

1

if your text is a list convert it to string using:

my_string = ','.join(your_list)

Below is python code

text = "[<JIRA Issue: key='HDDIS-42214', id='855344'>, <JIRA Issue: key='HDDIS-42171', id='854930'>, <JIRA Issue: key='HDDIS-42170', id='854929'>]"
import re    
m = re.findall("(?<=<)(.*?)(?=>)",text)
res = list()
for i in m:
   key = re.findall("(?<=key=')(.*?)(?=',)",i)
   res.append(key[0])

Your output would be: enter image description here

ratnesh
  • 436
  • 1
  • 5
  • 16
  • [Please do not upload images of code/data/errors when asking or answering a question.](http://meta.stackoverflow.com/q/285551) – martineau Apr 01 '22 at 11:03