python - regex or other way to fetch data from a line with variable entry -


In Python, the lines I am trying to fetch data from a line look like this:

  1. cpasite = 5 nsubl = 4 cpatypes = 3,4,5,6 2. cpasite = 6 nsubl = 2 cpatypes = 7,8 3. cpasite = 7 nsubl = 4 cpatypes = 9,10 4. cpasite = 8 nsubl = 2 cpatypes = 11,12 5. cpasite = 9 nsubl = 6 cpatypes = 13,14,15,16,17,18   

I've regexed it in this form :

  pattern = r '(\ d +) \. \ S * cpasite = (. *) \ S * nsubl = (. *) \ S * cpatypes = (. *) '  

The problem is, I need those sites (like 3, 4,5,6) different, so that I can use them for my purpose.

Why not use your regex and then the fourth captured group , Which will be as follows: '3,4,5,6'

You can divide that string to get the values ​​of your codes on , , Those you can work differently:

  s = '3,4,5,6' S = map (int, s.split (',')) print [3,4 , 5, 6] & gt; & Gt; & Gt; Print [2] 5    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -