0

I need help with Python string manipulation (I boiled down my previous looong question to this issue below).

For this line from a file:

L20B, CVS=1, HTYP=16, MLV=25

The second field could be CVS or VS. Related data will be present till the end of the line.

Need to replace the part starting with CVS or VS with another string:

if CVS found, then replacement is CFIXD(0,1,0) -OR-
if VS found, then replacement is FIXD(0,1,0)

Examples:

old line: L20B, CVS=1, HTYP=16, MLV=25
new line: L20B, CFIXD(0,1,0)

Old line: T10, M312, P10, Z3710, CL=L1, RH=1  (here, identify RH only and replace with)
New line: T10, M312, P10, Z3710, CL=L1, FIXD(0,1,0)

Old line: T20, M312, P20, Z100, CKR=10000 DV(0,1,0) 
New line: T20, M312, P20, Z100, CLS(0,1,0), MU=0.35

So, the replacement string keeps changing with what is found.
CVS or VS (till end of line) is replaced with CFIXD(0,1,0) or FIXD(0,1,0)
CRH or RH (till end of line) is replaced with CVR(0,1,0) or VR(0,1,0)
CFIXD or FIXD (till end of line) is replaced with CVR(0,1,0) or VR(0,1,0)
20 other variants.

Also, is it possible to modify the re.sub() expression to identify something in the search string and carry it over to the replacement string?
For e.g., 
Search for CFIXD(x,y,z) - replace with CVR (x,y,z) 

I cannot search for the exact substring ("CVS=1, HTYP=16, MLV=25") since the data after the CVS (or VS) could be many different variations like

CVS=2, HTYP=11, MLV=25 
VS=4, HTYP=9, MLV=5      etc. 

The length as you can see could be different too. Only thing I know for sure is that the string starting with CVS or VS goes till the end of that line. As far as I know, string.replace won't work because of the above varying length and data.

Any readily available Python methods? Or do I have to write a small routine to do this? I can find the index (using string.find) to the VS or CVS and then replace everything from that point to the end of the line, yes? I know there is a simple (not for me) regex way. Thanks.

2
  • 2
    Am I correct in understanding that you wish to grab everything after and including the characters "CVS=" or "VS="? It is not clear what needs to be replaced. Can you give example input and output strings? This should be a simple regex exercise. Commented Dec 13, 2012 at 19:52
  • just added examples to the main question area. Commented Dec 13, 2012 at 19:57

2 Answers 2

3

Use regular expressions:

import re
re.sub(r'(C|)VS=.*', r'\1FIXD(0,1,0)', line)

Explanation:

# regex
  (C|)         # optionally match a 'C', save it or an empty string in group 1
  VS=          # match 'VS='
  .*           # match to the end of the line

# replacement
  \1           # the contents of group 1 (either 'C' or an empty string)
  FIXD(0,1,0)  # the literal string 'FIXD(0,1,0)'

Examples:

>>> re.sub(r'(C|)VS=.*', r'\1FIXD(0,1,0)', 'L20B, CVS=1, HTYP=16, MLV=25')
'L20B, CFIXD(0,1,0)'
>>> re.sub(r'(C|)VS=.*', r'\1FIXD(0,1,0)', 'L20C, VS=4, HTYP=9, MLV=5')
'L20C, FIXD(0,1,0)'

Edit: based on your edit here are a few alternatives for your different cases:

  • CVS or VS -> CFIXD(0,1,0) or FIXD(0,1,0)

    re.sub(r'(C|)VS=.*', r'\1FIXD(0,1,0)', line)
    
  • CRH or RH -> CVR(0,1,0) or VR(0,1,0)

    re.sub(r'(C|)RH=.*', r'\1VR(0,1,0)', line)
    
  • CFIXD(x,y,z) or VIXD(x,y,z) -> CVR(x,y,z) or VR(x,y,z)

    re.sub(r'(C|)FIXD(\([^)]*\)).*', r'\1VR\2', line)
    

Explanation of (\([^)]\)).*:

(         # start second capture group
   \(       # match a literal '('
   [^)]*    # match any number of characters that are not ')'
   \)       # match a literal ')'
)         # end capture group
.*        # match to the end of the line
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, that looks simple. But, will it add the 'C' if it finds it?
Yes, but it is probably better to use a solution that you understand. If you have specific questions about how this works feel free to ask.
Actually, the last field (replacement) needs to be one of many choices. CVS/VS replace with FIXD() or CFIXD, RH/CRH replace with CLS()...... or LS()......., and so on. But, if I figure out the mechanism for one, I can handle the others.
Ahh got it, if you are having trouble adapting it to your other fields, you should be able to use re.sub(r'(C|)RH=.*', r'\1LS(0,1,0)', line) for the RH/CRH replacements.
Sorry, I just elaborated with more examples. Thank you kindly for assisting me.
|
1

You can use a regular expression, but to me it'd be more straightforward to just split the string into a list:

line = "L20B, CVS=1, HTYP=16, MLV=25"

line = line.split(", ")
if line[1].startswith("CVS="):
    line[1:] = ["CFIXD(0,1,0)"]
elif line[1].startswith("VS="):
    line[1:] = ["FIXD(0,1,0)"]

line = ", ".join(line)

These two cases could be combined with some jiggery-pokery, since they are rather similar, but they seem perfectly readable this way.

3 Comments

I agree but the problem is that VS could show up towards the end of the line. Example: T10,M312,P10,Z3710,CL=L1, VS=1,HTYP=16,MLV=25; in this case, I guess I could modify with a loop, say line[i].startswith("VS="). Is that right?
Yes, although you're starting to get into "regex is easier" territory here.
I like your solution but it gets verbose quickly with the 20 variants I have. But, it is understandable to a non-regex person like me. I will see how it goes. Thank you both for your quick help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.