简体   繁体   English

Python string.replace替代

[英]Python string.replace alternative

I need help with Python string manipulation (I boiled down my previous looong question to this issue below). 我需要有关Python字符串操作的帮助(我将下面的问题一直归结为下面的问题)。

For this line from a file: 对于文件中的这一行:

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

The second field could be CVS or VS. 第二个字段可以是CVS或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: 需要用另一个字符串替换以CVS或VS开头的部分:

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 = 1,HTYP = 16,MLV = 25”),因为CVS(或VS)之后的数据可能有很多不同的变化,例如

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. 我唯一确定的是,以CVS或VS开头的字符串会一直到该行的末尾。 As far as I know, string.replace won't work because of the above varying length and data. 据我所知,由于上述长度和数据的变化,string.replace无法正常工作。

Any readily available Python methods? 有任何现成的Python方法吗? 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? 我可以找到VS或CVS的索引(使用string.find),然后替换从该点到行尾的所有内容,是吗? 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) CVS或VS-> CFIXD(0,1,0)或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) CRH或RH-> CVR(0,1,0)或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) CFIXD(x,y,z)或VIXD(x,y,z)-> CVR(x,y,z)或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. 由于这两种情况相当相似,因此可以结合使用一些奇特的扑克游戏,但以这种方式看来它们可读性很强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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