Reputation: 31
Some tricky (for me) string manipulation required here. I have the following data:
Candidate solution = [4, 2, 3] (this can be any of {1,2,3,4})
Need to replace part of the line based on the criteria (farther) below. Only 3 example lines shown here. The string to replace is shown within the < b > bold tags in the code below. < b > tags are not part of the original.
Orig. Line # 34 ['T10', 'M312', 'P10', 'Z3710', 'CL=L1', '<b>RH=1</b>']
Orig. Line # 37 ['L20B', '<b>CVS=1', 'HTYP=16', 'MLV=25</b>']
Orig. Line # 48 ['L115B', '<b>CVS=1', 'HTYP=16', 'MLV=25</b>']
Criteria:
if Candidate[i] == 2:
modified line37 = "L20B, <b>CFIXD(0,1,0)</b>"
# so, replaced CVS=1, HTYP=16, MLV=25 with CFIXD(0,1,0)
if Candidate[i] == 3:
modified line48 = "L115B, <b>CCS=1</b>"
if Candidate[i] == 4:
modified line34 = "T10,M312,P10,Z3710,CL=L1, <b>CVS=1,HTYP=16,MLV=25</b>"
if Candidate[i] == 1:
modified linexx = whatever comes here
So, task is to replace a substring (or substr to the end of line) in a given line with "XY" or "CXY" depending on what is found in the original line.
The original lines could be in a couple of forms as shown below:
1a. T15,M1,P2,X4'6",CL=3,<b>FIXD(0,1,0)</b>
--OR--
1b. F15,<b>CFIXD(0,1,0)</b>
So, as can be seen, the "FIXD()" can show up as in item 1a or 1b. The main thing is, the replacement depends on what already exists: "FIXD" or "CFIXD" or "VS" or "CVS" (16 variants).
A few more line variants shown (actual str to be replaced shown b/w tags):
2a. T55,P3,X3'0",CL=2,<b>G,MU=0.500,STIFF=Rigid</b>
2b. T55,P3,X3'0",CL=2,<b>G,MU=0.500,STIFF=Rigid,GGAP=0.500</b>
3a. T123,JS,X2'0",CFFOR=5000,FTOR=500,WGT=0.5,<b>LS(0.000,None),DV(0.0000,1.0000,0.0000),STIFF=Rigid</b>
3b. L130,<b>CLS(0.000,0.250),DV(0.0000,1.0000,0.0000),STIFF=Rigid</b>
4. T124,X1'0",<b>CUS(1,0,0)</b>
5. T130,X1'0",Y1'0",<b>CRH=1</b>
6. F35,<b>CCS=1</b>
7. L40A,<b>CK=10000,DV(0.0000,1.0000,1.0000</b>
My approach is tending towards identifying the substring using XY or CXY, deleting everything to the end of the line, and replacing with the new string. I do not know Python enough to be clever abt it, tho'.
Thanks for your input.
Upvotes: 0
Views: 294
Reputation: 2430
Hopefully I understand the question correctly.
You should be able to use the string.replace() function easily enough. Assuming those are in fact strings, and not lists you could do it like this:
if Candidate[i] = 2:
if "CVS=1', 'HTYP=16', 'MLV=2" in input:
output = input.replace("CVS=1', 'HTYP=16', 'MLV=25", "CFIXD(0,1,0)")
continue
if "VS=1', 'HTYP=16', 'MLV=2" in input:
output = input.replace("VS=1', 'HTYP=16', 'MLV=25", "FIXD(0,1,0)")
continue
if Candidate[i] = 3:
if "CVS=1', 'HTYP=16', 'MLV=25" in input:
output = input.replace("CVS=1', 'HTYP=16', 'MLV=25", "CCS=1")
continue
Hopefully you get the idea.
Upvotes: 1