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

    #16
    I will look at this evening.

    BV

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #17
      This calls for a new (and simpler) approach:
      Code:
      f = open('data_file')
      idDict = {1:1000,2:1100,3:1150,4:5000}
      pntDict = { 1:5000,2:5100,3:5200,4:5003,5:6000,6:6500,7:9000,8  :7877,9:4500}
      outList = []
      for line in f:
          if line.startswith('Rect') or line.startswith('Tria'):
              target = line[8:16]
              if int(target) in idDict.keys():
                  line = ('%-8s' % idDict[int(target)]).join([line[0:8], line[16:]])
          if line.startswith('Pnt'):
              target = line[8:16]
              if int(target) in pntDict.keys():
                  line = ('%-8s' % pntDict[int(target)]).join([line[0:8], line[16:]])
          outList.append(line)
      f.close()
      print ''.join(outList)
      Output:
      Code:
      >>> $$$$$$$$$$$$$$$$$$$$$$
      Rect    1000    1       2       7       6
      Rect    1100    2       3       8       7
      Rect    1150    3       4       9       8
      Tria    5000    4       5       9
      Pnt     5000    0.      0.      0.
      Pnt     5100    5.      0.      0.
      Pnt     5200    10.     0.      0.
      Pnt     5003    15.     0.      0.
      Pnt     6000    20.     0.      0.
      Pnt     6500    0.      5.      0.
      Pnt     9000    5.      5.      0.
      Pnt     7877    10.     5.      0.
      Pnt     4500    15.     5.      0.
      
      $$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$
      >>> 
      >>>
      This solution is dependent upon a consistent format of 8 character fields.

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #18
        Code:
        SAmpleInputData
        $$$$$
        START
        COLOR RED
        LINETYPE SOLID
        END
        $$$$$$$
        PLine   1        6      1.5     9.375   .001    .001
        $ Line Details
        Line*   1               1                1              2
        *       .002952         .992547         .121827
        $
        
        Rect    2        1      2       3       7       6
        Rect    3        1      3       4       8       7
        PRect*  4               11              15              16
        *       10              11              0.3
        Rect*   4               1               5               6
        *       10              11              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
        Rect*   5               1               5               6
        *       10              11              0.
        Rect    10000000	10000000200000007000000060000000
        Rect    20000000	20000000300000008000000070000000
        Rect    30000000	30000000400000009000000080000000
        Tria    40000000	400000005000000090000000
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  $
        Tria    6        1      7       2       11
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  
        Point   1               0.0     0.0     0.0
        Point   2               1.0     0.0     0.0
        Point   3               2.0     0.0     0.0
        Point   4               3.0     0.0     0.0
        Point   5               0.0     1.0     0.0
        Point   6               1.0     1.0     0.0
        Point   7               2.0     1.0     0.0
        Point   8               4.0     1.0     0.0
        Point*  9                               0.0             2.0
        *       0.0
        Point  *3280504         0               1.28286145E+03  1.28286145E+03
        *       -2.01004501E+02
        Point  *3280505         0               1.28286145-03  1.28286145+03
        *       -2.01004501+02
        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.
        $
        END
        Same input file data is used as posted in the File Parsing thread.Need to replace with new IDs for Points and the Curves.
        We have 3-Cases

        a) Replace Point IDs only but reflect the same in CurveID fiellds
        b) Replace Curve IDs only but the PointiDs for the curves will be retained same
        c) Replace PointIDs and CurveIDs ( Both of them)

        Below Post Shows the 3 different scenarios

        Comment

        • psbasha
          Contributor
          • Feb 2007
          • 440

          #19
          Code:
          3-Cases
          Portion of the InputData:
          -----------------------
          PLine   1        6      1.5     9.375   .001    .001
          Line    1        1      1       2
          Rect    2        1      1       2       3       4
          PRect*  3               4               3               1
          *       10              11              0.3
          Rect*   3               1               4               3
          *       2               1               0.
          $$$
          Point   1               0.0     0.0     0.0
          Point   2               1.0     0.0     0.0
          Point*  3                               0.0             2.0
          *       0.0
          Point  *4              0               1.28286145E+03  1.28286145E+03
          *       -2.01004501E+02
          
          OutputData for 3-Cases:
          ---------------------
          
          Case-1( Only PointIDs has to be replaced):
          ----------------------------------------------
          
          START
          COLOR RED
          LINETYPE SOLID
          END
          PLine   1        6      1.5     9.375   .001    .001
          Line    1        1      8601010186010102    
          Rect    2        1      860101018601010286010103860101004
          PRect*  3               4               3               1
          *       10              11              0.3
          Rect*   3               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
          
          Case- 2( Only CurveIDs has to be replaced):
          ----------------------------------------------
              
          START
          COLOR RED
          LINETYPE SOLID
          END
          PLine   90010101 6      1.5     9.375   .001    .001
          Line    90010102 1      1       2
          Rect    90010103 1      1       2       3       4
          PRect*  90010104        4               3               1
          *       10              11              0.3
          Rect*   90010104        1               4               3
          *       2               1               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   1               0.0     0.0     0.0
          Point   2               1.0     0.0     0.0
          Point*  3                               0.0             2.0
          *       0.0
          Point  *4              0               1.28286145E+03  1.28286145E+03
          *       -2.01004501E+02
          
          Case-3(Both Point and Curve IDs has to be replaced):
           ----------------------------------------------
           
          START
          COLOR RED
          LINETYPE SOLID
          END
          PLine   90010101 6      1.5     9.375   .001    .001
          Line    90010102 1      8601010186010102    
          Rect    90010103 1      860101018601010286010103860101004
          PRect*  90010104        4               3               1
          *       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
          We will be having the PointIDMapDict, curveIDMapDict and curveIDAndPoint IdDict

          Comment

          • psbasha
            Contributor
            • Feb 2007
            • 440

            #20
            Code:
            SampleCode
            
            def write_new_pointID_to_file_for_small_format(strLine,pointID): # Change the Values
                line = ('%-8s' % pointID).join([strLine[0:8], strLine[16:]])
                return line
            
            def write_new_pointID_to_file_for_large_format(strLine,pointID): # Change the Values
                line = ('%-16s' % pointID).join([strLine[0:8], strLine[24:]])
                return line
                
            def write_new_curveID_to_file_for_small_format(strLine,curvetID,newCurveIdPointIDDict):
                # Currently supports for 8 digit field
                strNewPointID = ''
                newPointList = newCurveIdPointIDDict.get(curvetID)
                if newCurveIdPointIDDict:
                    line = ('%-8s' % curvetID).join([strLine[0:8], strLine[16:24]])
                    for i in range(0,len(newPointList)):
                        newPointID = newPointList[i]
                        if newPointID:
                            strNewPointID = strNewPointID + '%-8d' %newPointID
                    line = strNewPointID.join([line[0:24],line[24:]])
                else:
                    #Property
                    line = ('%-8s' % curvetID).join([strLine[0:8], strLine[16:]])
                    
                        
                return line
            
            def write_new_curveID_to_file_for_large_format(strLine,curvetID,newCurveIdPointIDDict):
                # Currently supports for 8 digit field
                strNewPointID = ''
                newPointList = newCurveIdPointIDDict.get(curvetID)
                if newCurveIdPointIDDict:
                    line = ('%-16s' % curvetID).join([strLine[0:8], strLine[24:40]])
                    pointList = newPointList [:2] # Only two elements
                    for i in range(0,len(pointList)):
                        newPointID = pointList[i]
                        if newPointID:
                            strNewPointID = strNewPointID + '%-16d' %newPointID
                    line = strNewPointID.join([line[0:72],line[72:]])
                else:
                    line = ('%-16s' % curvetID).join([strLine[0:8], strLine[24:]])
                    
                        
                return line
            
            def write_new_curveID_to_file_for_large_format_second_line(strLine,curvetID,newCurveIdPointIDDict):
                # Currently supports for 8 digit field
                strNewPointID = ''
                newPointList = newCurveIdPointIDDict.get(curvetID)
                line = strLine
                #line = ('%-8s' % *).join([strLine[0:8], strLine[40:]])
                pointList = newPointList [2:] # Only two elements
                for i in range(0,len(pointList)):
                    newPointID = pointList[i]
                    if newPointID:
                        strNewPointID = strNewPointID + '%-16d' %newPointID
                line = strNewPointID.join([line[0:8],line[40:]])
                    
                return line    
                
            def write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict):        
            
                fText = open(strFileName,'w')
                
                # True - Large Format
                # False - Small Format
                bLargeFormatFlag = True
                curvetID = 0
                
                bPropCardFlag = False
                bPropCardLargeFlag = False
                bCurveCardFlag = False
                bPointCardFlag = False
                bOtherDetailsFlag = False
                bCurveLargeFormat = False
                bPointLargeFormat = False
                b1DCurveFlag = False
            
                pointFlag = True
                curveFlag = True
                dataList = pointIDMapDict.keys()
                for i in range(0,len(dataList)):
                    if len(str(pointIDMapDict.get(dataList[i]))) > 8:
                        pointFlag = False
                        break
                    else:
                        pointFlag = True                
                dataList = curveIDMapDict.keys()   
                for i in range(0,len(dataList)):
                    if len(str(curveIDMapDict.get(dataList[i]))) > 8:            
                        curveFlag = False
                        break
                    else:
                        curveFlag = True
                        
                if  pointFlag and curveFlag :            
                    for line in listStrLine:
                        
                        if line.startswith('Point')and line[:8].strip().isalpha():
                            target = line[8:16]
                            if int(target) in pointIDMapDict.keys():
                                newPointID = pointIDMapDict[int(target)]
                                line = write_new_pointID_to_file_for_small_format(line,newPointID)
                                bPointCardFlag = True
                            bCurveLargeFormat = False
                        elif (line.startswith('Point*') or line.startswith('Point  *')):  #Large field format (say 16)
                            target = int(line[8:24]) 
                            if int(target) in pointIDMapDict.keys():
                                newPointID = pointIDMapDict[int(target)]
                                line = write_new_pointID_to_file_for_large_format(line,newPointID)
                                bPointCardFlag = True
                                bPointLargeFormat = True
                            bCurveLargeFormat = False
                        elif line.startswith('*') and bPointLargeFormat:
                             bPointCardFlag = True
                             bPointLargeFormat = False
                        # For Elements
                        elif(line.startswith('Rect  ')or \
                             line.startswith('Tria  ')  or \
                             line.startswith('Line') and line[:8].strip().isalpha() ):
                            target = line[8:16]
                            if int(target) in curveIDMapDict.keys():                    
                                newCurveID = curveIDMapDict[int(target)]
                                line = write_new_curveID_to_file_for_small_format(line,newCurveID,newCurveIdPointIDDict)
                                bCurveCardFlag = True
                            bCurveLargeFormat = False
                            bPointLargeFormat = False
                        elif(line.startswith('PLine') and line[:8].strip().isalpha() or\
                             line.startswith('PRect') and line[:8].strip().isalpha() ):
                            target = line[8:16]
                            if int(target) in curveIDMapDict.keys():
                                newCurveID = curveIDMapDict[int(target)]
                                dataDict = {}
                                line = write_new_curveID_to_file_for_small_format(line,newCurveID,dataDict)
                                bPropCardFlag = True
                            bPointLargeFormat = False
                        elif (line.startswith('Rect*') or \
                              line.startswith('Tria*') or \
                              line.startswith('Line*')):
                            target = line[8:24]
                            if int(target) in curveIDMapDict.keys():                    
                                newCurveID = curveIDMapDict[int(target)]
                                line = write_new_curveID_to_file_for_large_format(line,newCurveID,newCurveIdPointIDDict)
                                bCurveCardFlag = True
                                bCurveLargeFormat = True
                                curvetID = newCurveID
                                bPointLargeFormat = False
                            if line.startswith('Line*'):
                                b1DCurveFlag = True
                            else:
                                b1DCurveFlag = False
                        elif line.startswith('*') and bCurveLargeFormat :
                            if not b1DCurveFlag:
                                bCurveCardFlag = True
                                line = write_new_curveID_to_file_for_large_format_second_line(line,curvetID,newCurveIdPointIDDict)
                                bCurveCardFlag = True
                                bCurveLargeFormat = False
                                
                        elif(line.startswith('PLine*') or\
                             line.startswith('PRect*') ):
                            target = line[8:24]
                            dataDict = {}
                            if int(target) in curveIDMapDict.keys():
                                newCurveID = curveIDMapDict[int(target)]
                                line = write_new_curveID_to_file_for_large_format(line,newCurveID,dataDict)
                                bPropCardFlag = True
                                bPropCardLargeFlag = True                        
                            bCurveLargeFormat = False
                        elif line.startswith('*') and bPropCardLargeFlag:
                            bPropCardFlag = True
                            bPropCardLargeFlag = False 
                        else:
                            #if line.startswith('$') or line.startswith('*') :
                            if line.startswith('$') :
                                bOtherDetailsFlag = False                    
                            else:                    
                                # Header and Footer ,comments                    
                                bOtherDetailsFlag = True
                            b1DCurveFlag = False
                                    
                        if  bPropCardFlag or bCurveCardFlag or bPointCardFlag  or bOtherDetailsFlag or b1DCurveFlag :
                            fText.write(line.strip())
                            fText.write('\n')
                            bPropCardFlag = False
                            bCurveCardFlag = False
                            bPointCardFlag = False
                            bOtherDetailsFlag = False
                            #b1DCurveFlag = False
                    
                fText.close()
            if __name__ == '__main__':
            
                listStrLine =[]
                strFileName = 'C:\\Sample3_Mod.txt'
                f= open('C\\Sample3.txt','r')
                strTemp = f.readlines()
                
                for line in strTemp:
                    #Appending all the string data of BDF file.Needed this data for writing the Modified BDF file   
                    listStrLine.append(line)
                    pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                    curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                    newCurveIdPointIDDict = {10001101:[86010101,86010102],10001102:[86010102,86010103,86010110,96010104],10001103:[86010103,86010104,86010201,86010110],10001104:[86010106,96010104,86010115,96010403],10001106:[96010104,86010115,96010403]}
                
                write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict)
            Above sample code work for the Case-3 ( Replacing the IDs for Points and Curves)

            Need to implement for Case-1 and Case-3

            Is there anyway to modularize the above code to reduce so many lines of code and flag ussage.

            Thanks
            PSB

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #21
              BV,

              Can you help me to get out of these File writing data.

              Thanks
              PSB

              Comment

              • psbasha
                Contributor
                • Feb 2007
                • 440

                #22
                Code:
                Othercases
                if __name__ == '__main__':
                
                    listStrLine =[]
                    strFileName = 'C:\\Sample3_Mod.txt'
                    f= open('C:\\Sample3.txt','r')
                    strTemp = f.readlines()
                    
                    for line in strTemp:
                        listStrLine.append(line)
                        iCount = 2
                        
                        #Case-3
                        if iCount==3:
                
                            pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                            curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                            newCurveIdPointIDDict = {10001101:[86010101,86010102],10001102:[86010102,86010103,86010110,96010104],10001103:[86010103,86010104,86010201,86010110],10001104:[86010106,96010104,86010115,96010403],10001106:[96010104,86010115,96010403]}
                        elif iCount==1:
                            #Case-1
                            pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                            curveIDMapDict = {1:1,2:2,3:3,4:4,6:6}
                            newCurveIdPointIDDict = {1:[86010101,86010102],2:[86010102,86010103,86010110,96010104],3:[86010103,86010104,86010201,86010110],4:[86010106,96010104,86010115,96010403],6:[96010104,86010115,96010403]}
                        elif iCount==2:
                        #Case-2
                
                            #pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                            
                            pointIDMapDict = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,3280504:3280504}
                            curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                            newCurveIdPointIDDict = {10001101:[1,2],10001102:[2,3,7,5],10001103:[3,4,8,7],10001104:[5,6,9,3280504],10001106:[6,9,3280504]}
                            
                    
                    write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict)
                Last edited by psbasha; Dec 30 '07, 07:05 PM. Reason: Remove some content

                Comment

                • psbasha
                  Contributor
                  • Feb 2007
                  • 440

                  #23
                  Originally posted by psbasha
                  Code:
                  SampleCode
                  
                  def write_new_pointID_to_file_for_small_format(strLine,pointID): # Change the Values
                      line = ('%-8s' % pointID).join([strLine[0:8], strLine[16:]])
                      return line
                  
                  def write_new_pointID_to_file_for_large_format(strLine,pointID): # Change the Values
                      line = ('%-16s' % pointID).join([strLine[0:8], strLine[24:]])
                      return line
                      
                  def write_new_curveID_to_file_for_small_format(strLine,curvetID,newCurveIdPointIDDict):
                      # Currently supports for 8 digit field
                      strNewPointID = ''
                      newPointList = newCurveIdPointIDDict.get(curvetID)
                      if newCurveIdPointIDDict:
                          line = ('%-8s' % curvetID).join([strLine[0:8], strLine[16:24]])
                          for i in range(0,len(newPointList)):
                              newPointID = newPointList[i]
                              if newPointID:
                                  strNewPointID = strNewPointID + '%-8d' %newPointID
                          line = strNewPointID.join([line[0:24],line[24:]])
                      else:
                          #Property
                          line = ('%-8s' % curvetID).join([strLine[0:8], strLine[16:]])
                          
                              
                      return line
                  
                  def write_new_curveID_to_file_for_large_format(strLine,curvetID,newCurveIdPointIDDict):
                      # Currently supports for 8 digit field
                      strNewPointID = ''
                      newPointList = newCurveIdPointIDDict.get(curvetID)
                      if newCurveIdPointIDDict:
                          line = ('%-16s' % curvetID).join([strLine[0:8], strLine[24:40]])
                          pointList = newPointList [:2] # Only two elements
                          for i in range(0,len(pointList)):
                              newPointID = pointList[i]
                              if newPointID:
                                  strNewPointID = strNewPointID + '%-16d' %newPointID
                          line = strNewPointID.join([line[0:72],line[72:]])
                      else:
                          line = ('%-16s' % curvetID).join([strLine[0:8], strLine[24:]])
                          
                              
                      return line
                  
                  def write_new_curveID_to_file_for_large_format_second_line(strLine,curvetID,newCurveIdPointIDDict):
                      # Currently supports for 8 digit field
                      strNewPointID = ''
                      newPointList = newCurveIdPointIDDict.get(curvetID)
                      line = strLine
                      #line = ('%-8s' % *).join([strLine[0:8], strLine[40:]])
                      pointList = newPointList [2:] # Only two elements
                      for i in range(0,len(pointList)):
                          newPointID = pointList[i]
                          if newPointID:
                              strNewPointID = strNewPointID + '%-16d' %newPointID
                      line = strNewPointID.join([line[0:8],line[40:]])
                          
                      return line    
                      
                  def write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict):        
                  
                      fText = open(strFileName,'w')
                      
                      # True - Large Format
                      # False - Small Format
                      bLargeFormatFlag = True
                      curvetID = 0
                      
                      bPropCardFlag = False
                      bPropCardLargeFlag = False
                      bCurveCardFlag = False
                      bPointCardFlag = False
                      bOtherDetailsFlag = False
                      bCurveLargeFormat = False
                      bPointLargeFormat = False
                      b1DCurveFlag = False
                  
                      pointFlag = True
                      curveFlag = True
                      dataList = pointIDMapDict.keys()
                      for i in range(0,len(dataList)):
                          if len(str(pointIDMapDict.get(dataList[i]))) > 8:
                              pointFlag = False
                              break
                          else:
                              pointFlag = True                
                      dataList = curveIDMapDict.keys()   
                      for i in range(0,len(dataList)):
                          if len(str(curveIDMapDict.get(dataList[i]))) > 8:            
                              curveFlag = False
                              break
                          else:
                              curveFlag = True
                              
                      if  pointFlag and curveFlag :            
                          for line in listStrLine:
                              
                              if line.startswith('Point')and line[:8].strip().isalpha():
                                  target = line[8:16]
                                  if int(target) in pointIDMapDict.keys():
                                      newPointID = pointIDMapDict[int(target)]
                                      line = write_new_pointID_to_file_for_small_format(line,newPointID)
                                      bPointCardFlag = True
                                  bCurveLargeFormat = False
                              elif (line.startswith('Point*') or line.startswith('Point  *')):  #Large field format (say 16)
                                  target = int(line[8:24]) 
                                  if int(target) in pointIDMapDict.keys():
                                      newPointID = pointIDMapDict[int(target)]
                                      line = write_new_pointID_to_file_for_large_format(line,newPointID)
                                      bPointCardFlag = True
                                      bPointLargeFormat = True
                                  bCurveLargeFormat = False
                              elif line.startswith('*') and bPointLargeFormat:
                                   bPointCardFlag = True
                                   bPointLargeFormat = False
                              # For Elements
                              elif(line.startswith('Rect  ')or \
                                   line.startswith('Tria  ')  or \
                                   line.startswith('Line') and line[:8].strip().isalpha() ):
                                  target = line[8:16]
                                  if int(target) in curveIDMapDict.keys():                    
                                      newCurveID = curveIDMapDict[int(target)]
                                      line = write_new_curveID_to_file_for_small_format(line,newCurveID,newCurveIdPointIDDict)
                                      bCurveCardFlag = True
                                  bCurveLargeFormat = False
                                  bPointLargeFormat = False
                              elif(line.startswith('PLine') and line[:8].strip().isalpha() or\
                                   line.startswith('PRect') and line[:8].strip().isalpha() ):
                                  target = line[8:16]
                                  if int(target) in curveIDMapDict.keys():
                                      newCurveID = curveIDMapDict[int(target)]
                                      dataDict = {}
                                      line = write_new_curveID_to_file_for_small_format(line,newCurveID,dataDict)
                                      bPropCardFlag = True
                                  bPointLargeFormat = False
                              elif (line.startswith('Rect*') or \
                                    line.startswith('Tria*') or \
                                    line.startswith('Line*')):
                                  target = line[8:24]
                                  if int(target) in curveIDMapDict.keys():                    
                                      newCurveID = curveIDMapDict[int(target)]
                                      line = write_new_curveID_to_file_for_large_format(line,newCurveID,newCurveIdPointIDDict)
                                      bCurveCardFlag = True
                                      bCurveLargeFormat = True
                                      curvetID = newCurveID
                                      bPointLargeFormat = False
                                  if line.startswith('Line*'):
                                      b1DCurveFlag = True
                                  else:
                                      b1DCurveFlag = False
                              elif line.startswith('*') and bCurveLargeFormat :
                                  if not b1DCurveFlag:
                                      bCurveCardFlag = True
                                      line = write_new_curveID_to_file_for_large_format_second_line(line,curvetID,newCurveIdPointIDDict)
                                      bCurveCardFlag = True
                                      bCurveLargeFormat = False
                                      
                              elif(line.startswith('PLine*') or\
                                   line.startswith('PRect*') ):
                                  target = line[8:24]
                                  dataDict = {}
                                  if int(target) in curveIDMapDict.keys():
                                      newCurveID = curveIDMapDict[int(target)]
                                      line = write_new_curveID_to_file_for_large_format(line,newCurveID,dataDict)
                                      bPropCardFlag = True
                                      bPropCardLargeFlag = True                        
                                  bCurveLargeFormat = False
                              elif line.startswith('*') and bPropCardLargeFlag:
                                  bPropCardFlag = True
                                  bPropCardLargeFlag = False 
                              else:
                                  #if line.startswith('$') or line.startswith('*') :
                                  if line.startswith('$') :
                                      bOtherDetailsFlag = False                    
                                  else:                    
                                      # Header and Footer ,comments                    
                                      bOtherDetailsFlag = True
                                  b1DCurveFlag = False
                                          
                              if  bPropCardFlag or bCurveCardFlag or bPointCardFlag  or bOtherDetailsFlag or b1DCurveFlag :
                                  fText.write(line.strip())
                                  fText.write('\n')
                                  bPropCardFlag = False
                                  bCurveCardFlag = False
                                  bPointCardFlag = False
                                  bOtherDetailsFlag = False
                                  #b1DCurveFlag = False
                          
                      fText.close()
                  if __name__ == '__main__':
                  
                      listStrLine =[]
                      strFileName = 'C:\\Sample3_Mod.txt'
                      f= open('C\\Sample3.txt','r')
                      strTemp = f.readlines()
                      
                      for line in strTemp:
                          listStrLine.append(line)
                          pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                          curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                          newCurveIdPointIDDict = {10001101:[86010101,86010102],10001102:[86010102,86010103,86010110,96010104],10001103:[86010103,86010104,86010201,86010110],10001104:[86010106,96010104,86010115,96010403],10001106:[96010104,86010115,96010403]}
                      
                      write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict)
                  Above sample code work for the Case-3 ( Replacing the IDs for Points and Curves)

                  Need to implement for Case-1 and Case-3

                  Is there anyway to modularize the above code to reduce so many lines of code and flag ussage.

                  Thanks
                  PSB
                  Edited: Removed some conetent

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #24
                    Originally posted by psbasha
                    Code:
                    Othercases
                    if __name__ == '__main__':
                    
                        listStrLine =[]
                        strFileName = 'C:\\Sample3_Mod.txt'
                        f= open('C:\\Sample3.txt','r')
                        strTemp = f.readlines()
                        
                        for line in strTemp:
                            listStrLine.append(line)
                            iCount = 2
                            
                            #Case-3
                            if iCount==3:
                    
                                pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                                curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                                newCurveIdPointIDDict = {10001101:[86010101,86010102],10001102:[86010102,86010103,86010110,96010104],10001103:[86010103,86010104,86010201,86010110],10001104:[86010106,96010104,86010115,96010403],10001106:[96010104,86010115,96010403]}
                            elif iCount==1:
                                #Case-1
                                pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                                curveIDMapDict = {1:1,2:2,3:3,4:4,6:6}
                                newCurveIdPointIDDict = {1:[86010101,86010102],2:[86010102,86010103,86010110,96010104],3:[86010103,86010104,86010201,86010110],4:[86010106,96010104,86010115,96010403],6:[96010104,86010115,96010403]}
                            elif iCount==2:
                            #Case-2
                    
                                #pointIDMapDict = {1:86010101,2:86010102,3:86010103,4:86010104,5:86010106,6:96010104,7:86010110,8:86010201,9:86010115,3280504:96010403}
                                
                                pointIDMapDict = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,3280504:3280504}
                                curveIDMapDict = {1:10001101,2:10001102,3:10001103,4:10001104,6:10001106}
                                newCurveIdPointIDDict = {10001101:[1,2],10001102:[2,3,7,5],10001103:[3,4,8,7],10001104:[5,6,9,3280504],10001106:[6,9,3280504]}
                                
                        
                        write_data_file(strFileName,listStrLine,pointIDMapDict,curveIDMapDict,newCurveIdPointIDDict)
                    I have seen the following keywords in the data files: 'Point', 'Othr', 'Oth1', 'Rect', 'PRect', 'PLine', 'Line', 'Tria'. Obviously the point objects use the Point keyword. What keywords are associated with curve data objects?

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #25
                      You should consider a function that accepts the data dictionary, an ID replacement dictionary, and a keyword list, and returns a new dictionary.[code=Python]import copy
                      def replaceID(dataD ict, idDict, *args):
                      dd = copy.deepcopy(d ataDict)
                      for keyword in args:
                      if keyword in dd:
                      for item in dd[keyword]:
                      if item[0] in idDict:
                      item[0] = idDict[item[0]]
                      return dd[/code]Example using the output dictionary from the "any other best way of reading the file" thread:[code=Python]>>> dd = parseData(fn, *keywords)
                      >>> idDict1 = dict(zip(range( 1,11), range(60000001, 60000023, 2)))
                      >>> idDict2 = dict(zip(range( 1,11), range(60000002, 60000024, 2)))
                      >>> dd3 = replaceID(repla ceID(dd, idDict2, *['Line', 'Rect']), idDict1, *['Point',])
                      >>> for key in dd3:
                      ... print key
                      ... for item in dd3[key]:
                      ... print ' %s' % item
                      ...
                      Point
                      [60000001, 0.0, 0.0, 0.0]
                      [60000003, 1.0, 0.0, 0.0]
                      [60000005, 2.0, 0.0, 0.0]
                      [60000007, 3.0, 0.0, 0.0]
                      [60000009, 0.0, 1.0, 0.0]
                      [60000011, 1.0, 1.0, 0.0]
                      [60000013, 2.0, 1.0, 0.0]
                      [60000015, 4.0, 1.0, 0.0]
                      [60000017, 0.0, 2.0, 0.0]
                      [3280504, 0, 1282.8614500000 001, 1282.8614500000 001, -201.004501]
                      [3280505, 0, 0.0012828614500 000001, 1282.8614500000 001, -201.004501]
                      [10000000, 0.0, 0.0, 0.0]
                      [20000000, 5.0, 0.0, 0.0]
                      [30000000, 10.0, 0.0, 0.0]
                      [40000000, 15.0, 0.0, 0.0]
                      [50000000, 20.0, 0.0, 0.0]
                      [60000000, 0.0, 5.0, 0.0]
                      [70000000, 5.0, 5.0, 0.0]
                      [80000000, 10.0, 5.0, 0.0]
                      [90000000, 15.0, 5.0, 0.0]
                      PLine
                      [1, 6, 1.5, 9.375, 0.001, 0.001]
                      Tria
                      [40000000, 40000000, 50000000, 90000000]
                      [5, 1, 7, 2, 11]
                      PRect
                      [4, 11, 15, 16, 10, 11, 0.2999999999999 9999]
                      Line
                      [60000002, 1, 1, 2, 0.0029520000000 000002, 0.9925469999999 9996, 0.121827]
                      Oth1
                      [1, 1, 1, 5, 6, 10, 11, 0.0, 0.0, 10, 11, 0.0, 1.0, 10, 11, 0.0, 1.0, 10, 11, 0.0, 1.0]
                      Rect
                      [60000004, 1, 2, 3, 7, 6]
                      [60000006, 1, 3, 4, 8, 7]
                      [60000008, 1, 5, 6, 10, 11, 0.0]
                      [60000010, 1, 5, 6, 10, 11, 0.0]
                      [10000000, 10000000, 20000000, 70000000, 60000000]
                      [20000000, 20000000, 30000000, 80000000, 70000000]
                      [30000000, 30000000, 40000000, 90000000, 80000000]
                      Othr
                      [1, 1, 5, 6, 10, 11, 0.0, 0.0, 10, 11, 0.0, 1.0]
                      >>> [/code]Alternatively, you could directly modify the ID numbers in the output dictionary instead of making a deep copy and returning the new dictionary:[code=Python]def replaceID1(data Dict, idDict, *args):
                      for keyword in args:
                      if keyword in dataDict:
                      for item in dataDict[keyword]:
                      if item[0] in idDict:
                      item[0] = idDict[item[0]][/code]

                      Comment

                      • psbasha
                        Contributor
                        • Feb 2007
                        • 440

                        #26
                        Originally posted by bvdet
                        I have seen the following keywords in the data files: 'Point', 'Othr', 'Oth1', 'Rect', 'PRect', 'PLine', 'Line', 'Tria'. Obviously the point objects use the Point keyword. What keywords are associated with curve data objects?
                        Point it is 'Point'
                        Curves it is 'Line','Rect',' Tria' rtc

                        Others it is 'Othr','oth1' etc

                        Attributes it is 'Pline','Prect'

                        Thanks
                        PSB

                        Comment

                        • psbasha
                          Contributor
                          • Feb 2007
                          • 440

                          #27
                          >>> [/code]Alternatively, you could directly modify the ID numbers in the output dictionary instead of making a deep copy and returning the new dictionary:[code=Python]def replaceID1(data Dict, idDict, *args):
                          for keyword in args:
                          if keyword in dataDict:
                          for item in dataDict[keyword]:
                          if item[0] in idDict:
                          item[0] = idDict[item[0]][/code][/QUOTE]

                          But I need to write this to file for 3 different cases as mentioned above for the given input file.

                          How to replaces the data and write to the file?

                          Thanks
                          PSB

                          Comment

                          • psbasha
                            Contributor
                            • Feb 2007
                            • 440

                            #28
                            Originally posted by bvdet
                            I have seen the following keywords in the data files: 'Point', 'Othr', 'Oth1', 'Rect', 'PRect', 'PLine', 'Line', 'Tria'. Obviously the point objects use the Point keyword. What keywords are associated with curve data objects?

                            Refer to the above 3 to 4 postings of piece of code and 3 scenarios

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #29
                              I do not understand why this has to be so complicated. Why can you not write the data to a disc file from the dictionary with the converted ID numbers? Why not use a delimiter (comma, semi-colon, tab, etc.) instead of field widths of 8 or 16?

                              Comment

                              • psbasha
                                Contributor
                                • Feb 2007
                                • 440

                                #30
                                Originally posted by bvdet
                                I do not understand why this has to be so complicated. Why can you not write the data to a disc file from the dictionary with the converted ID numbers? Why not use a delimiter (comma, semi-colon, tab, etc.) instead of field widths of 8 or 16?
                                It is a fixed format generated as output from the software.This output file goes as input to our application,so after some process it will spit the output file similar to the Input file with the IDs edited.

                                I dont have rights to modify the file format.I have to follow the 8 or 16 digit format.

                                Any solution for writing the modularize code for the above 3 cases.For reference I have posted the sample code in the above discussions.

                                Thnaks
                                PSB

                                Comment

                                Working...