Difflib parsing

 
0
 

I have written this short code

import os
import os.path
import string
import shutil
import difflib

Path = 'C:/RESULT/BATCH/SimpleInputParser/'

SecPoint = []
FullList = []
newFile = []

for fileName in os.listdir(Path):
    if fileName.endswith(".inp"):
        f=open(Path + fileName,  'r')
        files = f.readlines()
        f.close()
        fp_clean = open(Path + "tempClean.inp", "w")
        fp_newFile = open(Path + "newFile.inp", "w")

        for i, line in enumerate(files):
            if line.startswith(("*SECTION POINTS")):
                SecPoint.append(files[i].strip())
                SecPoint.append(files[i+1].strip())
            newFile.append(line.strip())

        fp_clean.write('\n'.join(SecPoint).strip()+'\n')
        fp_clean.close()
        fp_newFile.write('\n'.join(newFile).strip())
        fp_newFile.close()

        f1 = open(Path + "newFile.inp", "r")
        fileOne = f1.readlines()
        f1.close()

        f2 = open(Path + "tempClean.inp", "r")
        fileTwo = f2.readlines()
        f2.close()

        outFile = open(Path + fileName.split(".")[0] + "_v01.inp", "w")

        diffInstance = difflib.Differ()
        diffList = list(diffInstance.compare(fileOne, fileTwo))

        for line in diffList:
            if line[0] =='-':
                FullList.append(line.split("-")[1].strip())

        outFile.write('\n'.join(FullList).strip())
        outFile.close()

        SecPoint = []
        FullList = []
        newFile = []
        
        if os.path.exists(Path + "tempClean.inp"):
            os.remove(Path + "tempClean.inp")
        else:
            break
        if os.path.exists(Path + "newFile.inp"):
            os.remove(Path + "newFile.inp")
        else:
            break

When I compare the outFile with the input, I have notice that python is substituting all the lines that starts with "-" (minus, negative numbers) with blank lines.

Can you tell me why and how to fix it? I have to use 2.6.2 python

Thanks

Attachments Rib37.txt (110.11 KB)
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
**
** ABAQUS Input Deck Generated by HyperMesh Version  : 11.0.0.47
** Generated using HyperMesh-Abaqus Template Version : 11.0.0.47
**
**   Template:  ABAQUS/STANDARD 3D
**
*NODE
   6037000,  25786.3        ,  21345.5        ,  2985.31        
   6037001,  25799.5        ,  21355.0        ,  2799.87        
   6037002,  25813.0        ,  21364.5        ,  2614.55        
   6037003,  25698.8        ,  21517.3        ,  2994.12        
   6037004,  25713.4        ,  21527.5        ,  2791.87        
   6037005,  25728.0        ,  21537.9        ,  2589.8         
   6037006,  25611.5        ,  21689.3        ,  2997.12        
   6037007,  25627.0        ,  21700.0        ,  2782.14        
   6037008,  25642.5        ,  21711.0        ,  2567.3         
   6037009,  25524.5        ,  21861.5        ,  2995.89        
   6037010,  25540.8        ,  21873.0        ,  2772.35        
   6037011,  25557.0        ,  21884.4        ,  2548.62        
   6037012,  25436.0        ,  22037.0        ,  2993.62        
   6037013,  25452.9        ,  22048.8        ,  2763.62        
   6037014,  25469.5        ,  22060.5        ,  2533.62        
   6037015,  25348.0        ,  22212.5        ,  2989.09        
   6037016,  25364.9        ,  22224.4        ,  2755.88        
   6037017,  25381.5        ,  22236.3        ,  2522.87        
   6037018,  25259.9        ,  22388.0        ,  2982.87        
   6037019,  25276.8        ,  22400.0        ,  2749.62        
   6037020,  25293.5        ,  22411.9        ,  2516.34        
   6037021,  25171.8        ,  22564.0        ,  2975.12        
   6037022,  25188.5        ,  22575.8        ,  2744.39        
   6037023,  25205.0        ,  22587.5        ,  2513.82        
   6037024,  25083.8        ,  22740.0        ,  2965.75        
   6037025,  25100.0        ,  22751.5        ,  2740.08        
   6037026,  25116.4        ,  22763.0        ,  2514.37        
   6037027,  24996.0        ,  22915.9        ,  2954.79        
   6037028,  25011.8        ,  22927.0        ,  2736.12        
   6037029,  25027.5        ,  22938.0        ,  2517.56        
   6037030,  24908.0        ,  23092.0        ,  2942.37        
   6037031,  24923.3        ,  23102.9        ,  2733.12        
   6037032,  24938.4        ,  23113.5        ,  2523.84        
   6037033,  24820.5        ,  23268.5        ,  2927.62        
   6037034,  24834.9        ,  23278.5        ,  2728.87        
   6037035,  24849.3        ,  23288.8        ,  2530.29        
   6037036,  24733.0        ,  23444.5        ,  2911.78        
   6037037,  24746.5        ,  23454.0        ,  2726.27        
   6037038,  24759.9        ,  23463.5        ,  2540.75        
   6037039,  24646.4        ,  23619.5        ,  2893.76        
   6037040,  24658.5        ,  23628.4        ,  2723.36        
   6037041,  24671.0        ,  23637.0        ,  2552.87        
   6037042,  24560.0        ,  23795.0        ,  2870.89        
   6037043,  24571.0        ,  23802.8        ,  2718.37        
   6037044,  24582.0        ,  23810.5        ,  2565.87        
   6103700,  25881.0        ,  21142.0        ,  3067.11        
   6103715,  25780.5        ,  21339.4        ,  3077.62        
   6103716,  25692.5        ,  21512.5        ,  3082.37        
   6103717,  25604.9        ,  21685.5        ,  3084.01        
   6103718,  25517.4        ,  21858.9        ,  3083.12        
   6103719,  25428.5        ,  22035.0        ,  3080.34        
   6103720,  25340.0        ,  22211.3        ,  3075.62        
   6103721,  25251.5        ,  22387.5        ,  3069.37        
   6103722,  25163.3        ,  22564.0        ,  3061.51        
   6103723,  25075.0        ,  22740.5        ,  3052.06        
   6103724,  24986.9        ,  22917.0        ,  3041.05        
   6103725,  24898.5        ,  23094.0        ,  3028.37        
   6103726,  24810.5        ,  23271.0        ,  3014.07        
   6103727,  24722.9        ,  23448.0        ,  2997.87        
   6103728,  24635.9        ,  23623.9        ,  2979.77        
   6103729,  24548.9        ,  23800.0        ,  2959.37        
   6103790,  24462.3        ,  23975.3        ,  2936.6         
   6103890,  25264.8        ,  24380.8        ,  2986.85        
   6203700,  25893.4        ,  21150.5        ,  2898.12        
   6203790,  24470.4        ,  23979.5        ,  2835.48        
   6303700,  25905.1        ,  21159.0        ,  2733.84        
   6303790,  24485.0        ,  23986.9        ,  2648.47        
   6403700,  25917.7        ,  21167.9        ,  2560.31        
   6403718,  25823.0        ,  21360.9        ,  2532.12        
   6403719,  25749.8        ,  21509.9        ,  2511.62        
   6403720,  25676.5        ,  21658.8        ,  2492.37        
   6403721,  25602.9        ,  21807.5        ,  2475.32        
   6403722,  25529.0        ,  21956.4        ,  2460.83        
   6403723,  25455.3        ,  22105.0        ,  2449.35        
   6403724,  25381.0        ,  22253.5        ,  2440.87        
   6403725,  25306.5        ,  22401.9        ,  2435.55        
   6403726,  25232.0        ,  22550.0        ,  2432.79        
   6403727,  25157.0        ,  22698.3        ,  2432.37        
   6403728,  25082.0        ,  22846.4        ,  2434.12        
   6403729,  25007.0        ,  22994.4        ,  2437.85        
   6403730,  24931.5        ,  23142.4        ,  2443.37        
   6403731,  24856.3        ,  23290.3        ,  2450.62        
   6403732,  24775.8        ,  23447.9        ,  2459.87        
   6403733,  24695.0        ,  23605.5        ,  2470.12        
   6403734,  24614.5        ,  23763.0        ,  2481.03        
   6403790,  24496.8        ,  23992.9        ,  2496.62        
   6992612,  25865.4        ,  21266.0        ,  2587.39        
   6992613,  25852.5        ,  21257.0        ,  2764.58        
   6992615,  25839.8        ,  21248.0        ,  2941.68        
   6992616,  25833.5        ,  21243.5        ,  3026.17        
   6992633,  24522.4        ,  23892.0        ,  2754.16        
   6992634,  24511.0        ,  23885.0        ,  2903.78        
   6992635,  24533.5        ,  23898.5        ,  2604.6         
   6992638,  24539.4        ,  23901.5        ,  2531.2         
   6992986,  25599.5        ,  21797.5        ,  2557.93        
   6992987,  25160.5        ,  22675.0        ,  2514.09        
**HWCOLOR COMP       7239    17
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537000
  66537000,   6037000,   6037001,   6403700
**HWCOLOR COMP       7240    20
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537001
  66537001,   6037001,   6037002,   6403700
**HWCOLOR COMP       7241    21
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537003
  66537003,   6037003,   6037004,   6403700
**HWCOLOR COMP       7242    24
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537004
  66537004,   6037004,   6037005,   6403700
**HWCOLOR COMP       7243    25
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537006
  66537006,   6037006,   6037007,   6403700
**HWCOLOR COMP       7244    28
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537007
  66537007,   6037007,   6037008,   6403700
**HWCOLOR COMP       7245    29
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537009
  66537009,   6037009,   6037010,   6403700
**HWCOLOR COMP       7246    33
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537010
  66537010,   6037010,   6037011,   6403700
**HWCOLOR COMP       7247    35
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537012
  66537012,   6037012,   6037013,   6403700
**HWCOLOR COMP       7248    37
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537013
  66537013,   6037013,   6037014,   6403700
**HWCOLOR COMP       7249    40
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537015
  66537015,   6037015,   6037016,   6403700
**HWCOLOR COMP       7250    41
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537016
  66537016,   6037016,   6037017,   6403700
**HWCOLOR COMP       7251    44
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537018
  66537018,   6037018,   6037019,   6403700
**HWCOLOR COMP       7252    48
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537019
  66537019,   6037019,   6037020,   6403700
**HWCOLOR COMP       7253    49
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537021
  66537021,   6037021,   6037022,   6403700
**HWCOLOR COMP       7254    52
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537022
  66537022,   6037022,   6037023,   6403700
**HWCOLOR COMP       7255    53
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537024
  66537024,   6037024,   6037025,   6403700
**HWCOLOR COMP       7256    56
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537025
  66537025,   6037025,   6037026,   6403700
**HWCOLOR COMP       7257    57
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537027
  66537027,   6037027,   6037028,   6403700
**HWCOLOR COMP       7258    60
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537028
  66537028,   6037028,   6037029,   6403700
**HWCOLOR COMP       7259    61
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537030
  66537030,   6037030,   6037031,   6403700
**HWCOLOR COMP       7260    64
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537031
  66537031,   6037031,   6037032,   6403700
**HWCOLOR COMP       7261     3
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537033
  66537033,   6037033,   6037034,   6403700
**HWCOLOR COMP       7262     4
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537034
  66537034,   6037034,   6037035,   6403700
**HWCOLOR COMP       7263     5
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537036
  66537036,   6037036,   6037037,   6403700
**HWCOLOR COMP       7264     6
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537037
  66537037,   6037037,   6037038,   6403700
**HWCOLOR COMP       7265     7
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537039
  66537039,   6037039,   6037040,   6403700
**HWCOLOR COMP       7266     8
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537040
  66537040,   6037040,   6037041,   6403700
**HWCOLOR COMP       7267     9
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537042
  66537042,   6037042,   6037043,   6403700
**HWCOLOR COMP       7268    13
*ELEMENT,TYPE=B31,ELSET=ES_PID_BAR_66537043
  66537043,   6037043,   6037044,   6403700
**HWCOLOR COMP      11424
 
0
 

I should have add, that if you scroll down to *ORIENTATION, you will see the following line

*ORIENTATION, NAME = OR_PSHELL_MCID_2101, DEFINITION = COORDINATES, SYSTEM = RECTANGULAR

3,0.0

while it should be:

*ORIENTATION, NAME = OR_PSHELL_MCID_2101, DEFINITION = COORDINATES, SYSTEM = RECTANGULAR
-100.0 ,0.0 ,0.0 ,0.0 ,-100.0 ,0.0 ,0.0 ,0.0 ,0.0
3,0.0

Thanks

 
0
 

I didn't run your code, but it seems to me that lines 45-47 remove the lines that start with "-". I suppose there was some reason why you wrote this block of code ?

 
0
 

Thanks for your response, but diffList is a list that uses "-" as identifier of all the differences between the 2 compared lists

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: