1

I would like to know how can I get from a string and using reg expressions all values until the comma starting from the end. See below example, I would like to get the value "CA 0.810" into a variable:

prue ="VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"

So far, I have the below code:

test = re.findall('([0-9]+)$',prue)
print test

However, I only get below output:

['810']

Could you please advise how can I get "CA 0.810" into the test variable?

mx0
  • 4,605
  • 10
  • 39
  • 47
Jose Manuel
  • 95
  • 2
  • 8

1 Answers1

0

You can do this using the split method. From the docs, it will:

Return a list of the words in the string, using sep as the delimiter string.

So if you can take your string:

prue = "VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"

you can do :

prue.split(",")

which will return a list of the strings split by the commas:

['VA=-0.850', '0.800;CA=-0.863', '0.800;SP=-0.860', '0.810;MO=-0.860', '0.810;SUN=MO -0.850', 'CA 0.810']

So if you just want the last item ('CA 0.8101') into a variable named test, you can just take the last element from the list by indexing with -1:

test = prue.split(",")[-1]

test is now: 'CA 0.810'

Hope this helps!

Joe Iddon
  • 18,600
  • 5
  • 29
  • 49