繁体   English   中英

Python string.replace替代

[英]Python string.replace alternative

我需要有关Python字符串操作的帮助(我将下面的问题一直归结为下面的问题)。

对于文件中的这一行:

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

第二个字段可以是CVS或VS。 相关数据将一直显示到该行的末尾。

需要用另一个字符串替换以CVS或VS开头的部分:

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

例子:

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) 

我无法搜索确切的子字符串(“ CVS = 1,HTYP = 16,MLV = 25”),因为CVS(或VS)之后的数据可能有很多不同的变化,例如

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

如您所见,长度也可能不同。 我唯一确定的是,以CVS或VS开头的字符串会一直到该行的末尾。 据我所知,由于上述长度和数据的变化,string.replace无法正常工作。

有任何现成的Python方法吗? 还是我必须编写一个小的例程来执行此操作? 我可以找到VS或CVS的索引(使用string.find),然后替换从该点到行尾的所有内容,是吗? 我知道有一个简单的(不适用于我)正则表达式方式。 谢谢。

使用正则表达式:

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

说明:

# 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)'

例子:

>>> 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)'

编辑:根据您的编辑,这里有几种不同情况的选择:

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

     re.sub(r'(C|)VS=.*', r'\\1FIXD(0,1,0)', line) 
  • 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)或VIXD(x,y,z)-> CVR(x,y,z)或VR(x,y,z)

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

(\\([^)]\\)).*

(         # 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

您可以使用正则表达式,但对我而言,将字符串拆分为一个列表会更简单:

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)

由于这两种情况相当相似,因此可以结合使用一些奇特的扑克游戏,但以这种方式看来它们可读性很强。

暂无
暂无

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

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