Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'1VR2', 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...