Replacing a number(ID) in the file with another number(ID)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #31
    The following is a separate application for replacing the ID numbers in a data file.
    [code=Python]def replaceID2(file List, idDict, *args):
    pattkey = re.compile('|'. join([r'\b(%s)' % item for item in args]))
    for i, line in enumerate(fileL ist):
    m = pattkey.match(l ine)
    if m:
    try:
    mnum = pattnum.search( line)
    id = int(mnum.group( 0))
    if id in idDict:
    s = str(idDict[id])
    fileList[i] = '%s%s%s' % (line[:mnum.start()], s, \
    line[mnum.start()+le n(s):])
    except:
    print 'No valid ID number found.'
    return fileList

    if __name__ == '__main__':
    fn = 'sample_data'
    pointID = {1:86010101,2:8 6010102,3:86010 103,4:86010104, \
    5:86010106,6:96 010104,7:860101 10,8:86010201,\
    9:86010115,3280 504:96010403}
    curveID = {1:10001101,2:1 0001102,3:10001 103,4:10001104, \
    6:10001106}
    fileList = open(fn).readli nes()
    replaceID2(file List, curveID, *['Line', 'Rect', 'Tria'])
    replaceID2(file List, pointID, *['Point',])
    f = open('sample_da ta1', 'w')
    f.write(''.join (fileList))
    f.close()[/code]

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #32
      Originally posted by bvdet
      The following is a separate application for replacing the ID numbers in a data file.
      [code=Python]def replaceID2(file List, idDict, *args):
      pattkey = re.compile('|'. join([r'\b(%s)' % item for item in args]))
      for i, line in enumerate(fileL ist):
      m = pattkey.match(l ine)
      if m:
      try:
      mnum = pattnum.search( line)
      id = int(mnum.group( 0))
      if id in idDict:
      s = str(idDict[id])
      fileList[i] = '%s%s%s' % (line[:mnum.start()], s, \
      line[mnum.start()+le n(s):])
      except:
      print 'No valid ID number found.'
      return fileList

      if __name__ == '__main__':
      fn = 'sample_data'
      pointID = {1:86010101,2:8 6010102,3:86010 103,4:86010104, \
      5:86010106,6:96 010104,7:860101 10,8:86010201,\
      9:86010115,3280 504:96010403}
      curveID = {1:10001101,2:1 0001102,3:10001 103,4:10001104, \
      6:10001106}
      fileList = open(fn).readli nes()
      replaceID2(file List, curveID, *['Line', 'Rect', 'Tria'])
      replaceID2(file List, pointID, *['Point',])
      f = open('sample_da ta1', 'w')
      f.write(''.join (fileList))
      f.close()[/code]
      The numbers whose IDs are not to be replaced are not written in correct format to the output file
      Code:
      Data
      
      #Input data
      Rect    10000000	10000000200000007000000060000000
      Rect    20000000	20000000300000008000000070000000
      Rect    30000000	30000000400000009000000080000000
      Tria    40000000	400000005000000090000000
      
      Point   10000000	0.      0.      0.
      Point   20000000	5.      0.      0.
      Point   30000000	10.     0.      0.
      Point   40000000	15.     0.      0.
      Point   50000000	20.     0.      0.
      Point   60000000	0.      5.      0.
      Point   70000000	5.      5.      0.
      Point   80000000	10.     5.      0.
      Point   90000000	15.     5.      0.
      
      #Outpu file data ( Incorrect output data)
      
      Rect    10000000	10000000200000007000000060000000
      Rect    20000000	20000000300000008000000070000000
      Rect    30000000	30000000400000009000000080000000
      Tria    40000000	400000005000000090000000
      
      Point   10000000	0.      0.      0.
      Point   20000000	5.      0.      0.
      Point   30000000	10.     0.      0.
      Point   40000000	15.     0.      0.
      Point   50000000	20.     0.      0.
      Point   60000000	0.      5.      0.
      Point   70000000	5.      5.      0.
      Point   80000000	10.     5.      0.
      Point   90000000	15.     5.      0.

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #33
        Originally posted by psbasha
        The numbers whose IDs are not to be replaced are not written in correct format to the output file
        Code:
        Data
        
        #Input data
        Rect    10000000	10000000200000007000000060000000
        Rect    20000000	20000000300000008000000070000000
        Rect    30000000	30000000400000009000000080000000
        Tria    40000000	400000005000000090000000
        
        Point   10000000	0.      0.      0.
        Point   20000000	5.      0.      0.
        Point   30000000	10.     0.      0.
        Point   40000000	15.     0.      0.
        Point   50000000	20.     0.      0.
        Point   60000000	0.      5.      0.
        Point   70000000	5.      5.      0.
        Point   80000000	10.     5.      0.
        Point   90000000	15.     5.      0.
        
        #Outpu file data ( Incorrect output data)
        
        Rect    10000000	10000000200000007000000060000000
        Rect    20000000	20000000300000008000000070000000
        Rect    30000000	30000000400000009000000080000000
        Tria    40000000	400000005000000090000000
        
        Point   10000000	0.      0.      0.
        Point   20000000	5.      0.      0.
        Point   30000000	10.     0.      0.
        Point   40000000	15.     0.      0.
        Point   50000000	20.     0.      0.
        Point   60000000	0.      5.      0.
        Point   70000000	5.      5.      0.
        Point   80000000	10.     5.      0.
        Point   90000000	15.     5.      0.
        Ignore the above posting it was incorrect data in th einput file.

        Comment

        • psbasha
          Contributor
          • Feb 2007
          • 440

          #34
          Originally posted by bvdet
          The following is a separate application for replacing the ID numbers in a data file.
          [code=Python]def replaceID2(file List, idDict, *args):
          pattkey = re.compile('|'. join([r'\b(%s)' % item for item in args]))
          for i, line in enumerate(fileL ist):
          m = pattkey.match(l ine)
          if m:
          try:
          mnum = pattnum.search( line)
          id = int(mnum.group( 0))
          if id in idDict:
          s = str(idDict[id])
          fileList[i] = '%s%s%s' % (line[:mnum.start()], s, \
          line[mnum.start()+le n(s):])
          except:
          print 'No valid ID number found.'
          return fileList

          if __name__ == '__main__':
          fn = 'sample_data'
          pointID = {1:86010101,2:8 6010102,3:86010 103,4:86010104, \
          5:86010106,6:96 010104,7:860101 10,8:86010201,\
          9:86010115,3280 504:96010403}
          curveID = {1:10001101,2:1 0001102,3:10001 103,4:10001104, \
          6:10001106}
          fileList = open(fn).readli nes()
          replaceID2(file List, curveID, *['Line', 'Rect', 'Tria'])
          replaceID2(file List, pointID, *['Point',])
          f = open('sample_da ta1', 'w')
          f.write(''.join (fileList)
          f.close()[/code]
          I dont want the Curves or Points data to be written to the output file, if we dont want to replace the ID's

          The ouput should looks like below.Comments can be removed

          Code:
          Output
          START
          COLOR RED
          LINETYPE SOLID
          END
          PLine   1        6      1.5     9.375   .001    .001
          Line    90010102 1      8601010186010102    
          Rect    90010103 1      860101018601010286010103860101004
          PRect*  4               11              15              16
          *       10              11              0.3
          Rect*   90010104        1               86010104        86010103
          *       86010102        86010101               0.
          Othr*   1               1               5               6
          *       10              11              0.              0.
          *       10              11              0.              1.0
          Oth1*   1               1               5               6
          *       10              11              0.              0.
          *       10              11              0.              1.0
          *       10              11              0.              1.0
          *       10              11              0.              1.0
           
          Point   86010101               0.0     0.0     0.0
          Point   86010102               1.0     0.0     0.0
          Point*  86010103                       0.0             2.0
          *       0.0
          Point  *86010104       0               1.28286145E+03  1.28286145E+03
          *       -2.01004501E+02

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #35
            @OP, you should pay bv for doing your work.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #36
              Originally posted by psbasha
              I dont want the Curves or Points data to be written to the output file, if we dont want to replace the ID's

              The ouput should looks like below.Comments can be removed

              Code:
              Output
              START
              COLOR RED
              LINETYPE SOLID
              END
              PLine   1        6      1.5     9.375   .001    .001
              Line    90010102 1      8601010186010102    
              Rect    90010103 1      860101018601010286010103860101004
              PRect*  4               11              15              16
              *       10              11              0.3
              Rect*   90010104        1               86010104        86010103
              *       86010102        86010101               0.
              Othr*   1               1               5               6
              *       10              11              0.              0.
              *       10              11              0.              1.0
              Oth1*   1               1               5               6
              *       10              11              0.              0.
              *       10              11              0.              1.0
              *       10              11              0.              1.0
              *       10              11              0.              1.0
               
              Point   86010101               0.0     0.0     0.0
              Point   86010102               1.0     0.0     0.0
              Point*  86010103                       0.0             2.0
              *       0.0
              Point  *86010104       0               1.28286145E+03  1.28286145E+03
              *       -2.01004501E+02
              You need to make an effort to help yourself, instead of asking me to program for you. I suggest that you create a filter function that removes the unwanted lines from fileList before writing to disc. Please post back your solution. I have my own projects to work on.

              Comment

              • psbasha
                Contributor
                • Feb 2007
                • 440

                #37
                Originally posted by bvdet
                You need to make an effort to help yourself, instead of asking me to program for you. I suggest that you create a filter function that removes the unwanted lines from fileList before writing to disc. Please post back your solution. I have my own projects to work on.
                Here is the solution:

                Code:
                Filter
                def filter_numberIDs(fileList, idDict, *args):
                    astreikFlag = False
                    notNumFlag = False
                        
                    pattkey = re.compile('|'.join([r'\b(%s)' % item for item in args]))    
                    iLen = len(fileList)
                    fileList1 = []
                    for i in range(0,iLen):    
                        line = fileList[i]
                        if line.startswith('$'):
                            pass            
                        else:
                            m = pattkey.match(line)
                            if m:
                                try:
                                    mnum = pattnum.search(line)
                                    id = int(mnum.group(0))
                                    if id not in idDict.values():                         
                                        #astreikFlag = False
                                        if '*' in line:
                                            astreikFlag = False                            
                                        else:
                                            pass
                                        notNumFlag = True
                                    else:
                                        if not astreikFlag:
                                            fileList1.append(line)                            
                                            if '*' in line:
                                                astreikFlag = True                                
                                            else:
                                                astreikFlag = False
                                        notNumFlag = False
                                except:
                                    pass
                            else:
                                if astreikFlag:
                                    fileList1.append(line)
                                    astreikFlag = False                
                                    
                                else:
                                    if not astreikFlag and not notNumFlag:
                                        fileList1.append(line)
                                    notNumFlag = False
                    return fileList1
                    
                if __name__ == '__main__':
                    fn = 'sample4.txt'
                    pointID = {1:86010101,2:86010102,3:86010103,4:86010104,\
                               5:86010106,6:96010104,7:86010110,8:86010201,\
                               9:86010115,3280504:96010403}
                    curveID = {1:10001101,2:10001102,3:10001103,4:10001104,\
                               6:10001106}
                    fileList = open(fn).readlines()
                    replaceID2(fileList, curveID, *['Line', 'Rect', 'Tria'])
                    replaceID2(fileList, pointID, *['Point',])
                    newFileList = []
                
                    newFileList = filter_numberIDs(fileList, curveID, *['Line', 'Rect', 'Tria'])
                    newFileList = filter_numberIDs(newFileList, pointID, *['Point'])
                    
                        
                    f = open('sample_data1.txt', 'w')
                    f.write(''.join(newFileList))
                    f.close()
                Let me know whether this code can be made more modular.

                Thanks
                PSB

                Comment

                • psbasha
                  Contributor
                  • Feb 2007
                  • 440

                  #38
                  BV,

                  Please let me know your suggestion on this code

                  Thanks
                  PSB

                  Comment

                  Working...