0

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.

5
  • 1
    I am sorry, but I just read your post over and over again and still don't understand what you want to do? Could you provide some real input and the expected output for it? Could you state clearly what your substitution rules are? Commented Dec 11, 2012 at 1:52
  • I am sorry, I am trying to be as generic as possible without confusing the issue. Basically, I am preparing a text file for further processing. The original file has abt 10-20 lines which will require editing/replacement based on some conditions as detailed above. I identify the line to be modified, and now want to write the replacement line, and save all to a file. Hoping this is clearer. Thanks. Commented Dec 11, 2012 at 2:22
  • 1
    So is your question about how to modify lines of a file into a new output file, or about how to match and replace the lines? Also, this example code is pretty far from generic. It looks like The Matrix to me. Commented Dec 11, 2012 at 2:39
  • Jdi, you are funny. My question is about "how to match and replace the lines" as shown. Thanks. Commented Dec 11, 2012 at 3:10
  • Clarify your question into the simplest possible form, and don't add information that isn't part of the source (like your <b> tags, or the line numbers), which don't make any sense in this context and only confuse the matter. Commented Dec 11, 2012 at 5:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you---let me try that.
In your input.replace("string", "CFIXD(0,1,0)") function, the "string" will only begin with CVS or VS (for sure) but can have other parts not in "string". But I also know that this string WILL go till end of the line. I want to replace the "string" starting with "CVS or VS till end of line" and then in its place put in the new "CFIXD(0,1,0)" or "FIXD(0,1,0)" without any leftover from perv srting. I looked into regex but I could not figure out a simple way.
Thank you all for your comments. The updated simpler question and replies are here. stackoverflow.com/questions/13867282/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.