简体   繁体   中英

Python string.replace alternative

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM