Categories
Mastering Development

Python code to read text file and change data

I need a help in writing a python code that can read from a text file called sample.txt and modify the elements in the text file. I have put in sample text and the result that I expect from it.
The text file I want to read has data in the format below which is continuous. I have only put 3 sets for the sake of simplicity.

I want python to read my text file called sample.txt and change some columns in the text file in two parts.

  1. I want within the square brackets, everything below columns PER, PTT and TTT should be changed to UNSET. Also I want another column called “Direction” to be added at the end and all values under it should be set to "K" as shown below (Under RESULT section) and the arrays should match the column.
  2. Everything under completion that has the word fractures should have a Skin of 0 under column 7.

The sample data is shown below. Note that the data is all on the same file. Thank you for your anticipated help on this.

############################ TASK #######################

Sample "Test1" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius    Skin    PiMultiplier                    PER                       PTT                       TTT
     (139 95 4 Fracture)     "Completion (139 95 4 Fracture)"           1      OPEN     13279.996831266             0.349       -5               1            153.144806340198           2.944100548364     0.00342694483584852
     (139 95 5 MATRIX)     "Completion (139 95 5 MATRIX)"               1      OPEN     13298.456164437             0.349       -5               1            153.145310375351         2.78856848103508     0.00324590313033364
     (139 95 6 Fracture)     "Completion (139 95 6 Fracture)"           1      OPEN    13314.8611931295             0.349       -5               1            153.144848463809         3.43460791578149     0.00399789719633305
     (139 95 7 MATRIX)     "Completion (139 95 7 MATRIX)"               1      OPEN    13340.2535894277             0.349       -5               1            153.126939868365     0.000310997382359525    3.62009141334693E-07
     (139 95 8 MATRIX)     "Completion (139 95 8 MATRIX)"               1      OPEN    13365.6435474336             0.349       -5               1            153.146426293509          2.5634630162415      0.0029838760353557
     (139 95 9 MATRIX)     "Completion (139 95 9 MATRIX)"               1      OPEN    13385.3283624911             0.349       -5               1            153.127117447478     0.000196894526276026    2.29190368945904E-07
    (139 95 10 MATRIX)    "Completion (139 95 10 MATRIX)"               1      OPEN    13399.4533780681             0.349       -5               1            153.142786967734          1.0086201960757     0.00117404049448019
    (139 95 11 MATRIX)    "Completion (139 95 11 MATRIX)"               1      OPEN    13413.5761942839             0.349       -5               1            153.143331829475         4.35294647840879     0.00506685516240905
    (139 95 12 MATRIX)    "Completion (139 95 12 MATRIX)"               1      OPEN    13433.2610085465             0.349       -5               1            153.143134949541         8.43902946292755     0.00982308173264349
    (139 95 13 Fracture)    "Completion (139 95 13 Fracture)"           1      OPEN     13459.763348929             0.349       -5               1            153.146087338711         3.65174277820363     0.00425063738311018

]

}

Sample "Test2" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius      Skin    PiMultiplier                   PER                         PTT                 TTT
   (139 95 4 Fracture)     "Completion (139 95 4 Fracture)"              1      OPEN     13279.996831266             0.349       -5               1            153.144806340198           2.944100548364     0.00342694483584852
   (139 95 5 MATRIX)       "Completion (139 95 5 MATRIX)"                1      OPEN     13298.456164437             0.349       -5               1            153.145310375351         2.78856848103508     0.00324590313033364
   (139 95 6 MATRIX)       "Completion (139 95 6 MATRIX)"                1      OPEN    13314.8611931295             0.349       -5               1            153.144848463809         3.43460791578149     0.00399789719633305
   (139 95 7 MATRIX)       "Completion (139 95 7 MATRIX)"                1      OPEN    13340.2535894277             0.349       -5               1            153.126939868365     0.000310997382359525    3.62009141334693E-07
   (139 95 8 MATRIX)       "Completion (139 95 8 MATRIX)"                1      OPEN    13365.6435474336             0.349       -5               1            153.146426293509          2.5634630162415      0.0029838760353557
   (139 95 9 MATRIX)       "Completion (139 95 9 MATRIX)"                1      OPEN    13385.3283624911             0.349       -5               1            153.127117447478     0.000196894526276026    2.29190368945904E-07
  (139 95 10 MATRIX)      "Completion (139 95 10 MATRIX)"                1      OPEN    13399.4533780681             0.349       -5               1            153.142786967734          1.0086201960757     0.00117404049448019
  (139 95 11 MATRIX)      "Completion (139 95 11 MATRIX)"                1      OPEN    13413.5761942839             0.349       -5               1            153.143331829475         4.35294647840879     0.00506685516240905
  (139 95 12 MATRIX)      "Completion (139 95 12 MATRIX)"                1      OPEN    13433.2610085465             0.349       -5               1            153.143134949541         8.43902946292755     0.00982308173264349
  (139 95 13 MATRIX)      "Completion (139 95 13 MATRIX)"                1      OPEN     13459.763348929             0.349       -5               1            153.146087338711         3.65174277820363     0.00425063738311018
  (139 95 11 MATRIX)      "Completion (139 95 11 MATRIX)"                1      OPEN    13413.5761942839             0.349       -5               1            153.143331829475         4.35294647840879     0.00506685516240905
  (139 95 12 MATRIX)      "Completion (139 95 12 MATRIX)"                1      OPEN    13433.2610085465             0.349       -5               1            153.143134949541         8.43902946292755     0.00982308173264349
  (139 95 13 Fracture)    "Completion (139 95 13 Fracture)"              1      OPEN     13459.763348929             0.349       -5               1            153.146087338711         3.65174277820363     0.00425063738311018

]

}

Sample "Test3" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius    Skin    PiMultiplier                     PER                         PTT                    TTT
     (139 95 4 MATRIX)        "Completion (139 95 4 MATRIX)"              1      OPEN     13279.996831266             0.349       -5               1            153.144806340198           2.944100548364     0.00342694483584852
     (139 95 5 MATRIX)        "Completion (139 95 5 MATRIX)"              1      OPEN     13298.456164437             0.349       -5               1            153.145310375351         2.78856848103508     0.00324590313033364
     (139 95 6 Fracture)      "Completion (139 95 6 Fracture)"            1      OPEN    13314.8611931295             0.349       -5               1            153.144848463809         3.43460791578149     0.00399789719633305
     (139 95 7 MATRIX)        "Completion (139 95 7 MATRIX)"              1      OPEN    13340.2535894277             0.349       -5               1            153.126939868365     0.000310997382359525    3.62009141334693E-07
     (139 95 8 MATRIX)        "Completion (139 95 8 MATRIX)"              1      OPEN    13365.6435474336             0.349       -5               1            153.146426293509          2.5634630162415      0.0029838760353557

]
}

################################### Result ############################################

Sample "Test1" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius    Skin    PiMultiplier               PER                       PTT                        TTT                  Direction 
     (139 95 4 Fracture)     "Completion (139 95 4 Fracture)"            1      OPEN     13279.996831266             0.349       0               1            UNSET                     UNSET                       UNSET                      K  
     (139 95 5 MATRIX)       "Completion (139 95 5 MATRIX)"              1      OPEN     13298.456164437             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
     (139 95 6 Fracture)     "Completion (139 95 6 Fracture)"            1      OPEN    13314.8611931295             0.349       0               1            UNSET                     UNSET                       UNSET                      K  
     (139 95 7 MATRIX)       "Completion (139 95 7 MATRIX)"              1      OPEN    13340.2535894277             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
     (139 95 8 MATRIX)       "Completion (139 95 8 MATRIX)"              1      OPEN    13365.6435474336             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
     (139 95 9 MATRIX)       "Completion (139 95 9 MATRIX)"              1      OPEN    13385.3283624911             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
    (139 95 10 MATRIX)      "Completion (139 95 10 MATRIX)"              1      OPEN    13399.4533780681             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
    (139 95 11 MATRIX)      "Completion (139 95 11 MATRIX)"              1      OPEN    13413.5761942839             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
    (139 95 12 MATRIX)      "Completion (139 95 12 MATRIX)"              1      OPEN    13433.2610085465             0.349      -5               1            UNSET                     UNSET                       UNSET                      K  
    (139 95 13 Fracture)    "Completion (139 95 13 Fracture)"            1      OPEN     13459.763348929             0.349       0               1            UNSET                     UNSET                       UNSET                      K  

]

}

Sample "Test2" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius      Skin    PiMultiplier              PER                      PTT                        TTT                  Direction 
     (139 95 4 Fracture)     "Completion (139 95 4 Fracture)"          1      OPEN     13279.996831266             0.349        0               1             UNSET                     UNSET                       UNSET                      K  
     (139 95 5 MATRIX)     "Completion (139 95 5 MATRIX)"              1      OPEN     13298.456164437             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
     (139 95 6 MATRIX)     "Completion (139 95 6 MATRIX)"              1      OPEN    13314.8611931295             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
     (139 95 7 MATRIX)     "Completion (139 95 7 MATRIX)"              1      OPEN    13340.2535894277             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
     (139 95 8 MATRIX)     "Completion (139 95 8 MATRIX)"              1      OPEN    13365.6435474336             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
     (139 95 9 MATRIX)     "Completion (139 95 9 MATRIX)"              1      OPEN    13385.3283624911             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
    (139 95 10 MATRIX)    "Completion (139 95 10 MATRIX)"              1      OPEN    13399.4533780681             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
    (139 95 11 MATRIX)    "Completion (139 95 11 MATRIX)"              1      OPEN    13413.5761942839             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
    (139 95 12 MATRIX)    "Completion (139 95 12 MATRIX)"              1      OPEN    13433.2610085465             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
    (139 95 13 MATRIX)    "Completion (139 95 13 MATRIX)"              1      OPEN     13459.763348929             0.349       -5               1             UNSET                     UNSET                       UNSET                      K  
    (139 95 11 MATRIX)    "Completion (139 95 11 MATRIX)"              1      OPEN    13413.5761942839             0.349       -5               1             UNSET                     UNSET                       UNSET                      K
    (139 95 12 MATRIX)    "Completion (139 95 12 MATRIX)"              1      OPEN    13433.2610085465             0.349       -5               1             UNSET                     UNSET                       UNSET                      K
    (139 95 13 Fracture)    "Completion (139 95 13 Fracture)"          1      OPEN     13459.763348929             0.349        0               1             UNSET                     UNSET                       UNSET                      K

]

}

Sample "Test3" {
Undefined="False"

WellToCellConnections  [
                  Cell                         Completion    SegmentNode    Status       MeasuredDepth    WellBoreRadius    Skin    PiMultiplier             PER                         PTT                         TTT                Direction  
     (139 95 4 MATRIX)     "Completion (139 95 4 MATRIX)"              1      OPEN     13279.996831266             0.349       -5               1           UNSET                       UNSET                       UNSET                      K       
     (139 95 5 MATRIX)     "Completion (139 95 5 MATRIX)"              1      OPEN     13298.456164437             0.349       -5               1           UNSET                       UNSET                       UNSET                      K       
     (139 95 6 Fracture)     "Completion (139 95 6 Fracture)"          1      OPEN    13314.8611931295             0.349        0               1           UNSET                       UNSET                       UNSET                      K       
     (139 95 7 MATRIX)     "Completion (139 95 7 MATRIX)"              1      OPEN    13340.2535894277             0.349       -5               1           UNSET                       UNSET                       UNSET                      K       
     (139 95 8 MATRIX)     "Completion (139 95 8 MATRIX)"              1      OPEN    13365.6435474336             0.349       -5               1           UNSET                       UNSET                       UNSET                      K       

]

}

Leave a Reply

Your email address will not be published. Required fields are marked *