We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Difflib parsing

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)
2
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
maddocspace
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

maddocspace
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 ?

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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

maddocspace
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0653 seconds using 2.67MB