1.11M Members

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.11KB)
 
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: