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

Removing white space HELP

I have a code that takes a file and splits the file into two files. The problem I am having is when writing the lower half of the file I have two lines of whitespace I need removed. Here is what I have and am wonder how I could add a line of code to remove the extra lines.

Files=glob.glob('/home/mypathname/*.TAB')
output= ('/home/mypathname/')
for (count, aFileName) in enumerate(files):

    filename = aFileName.split('/')[11]
    day=aFileName.split('/')[10] 
    print day
    print filename
    F=open(aFileName, 'r')#.readlines() 

    _,keep=F.read().split('END ',1)
    #keep,_=F.read().split('END ',1)
    print keep 


    new=open(output+('/')+day+('/')+filename+('test'), 'w')

    for line in keep:

        new.write(line)
    #new.write("END")


    new.close()
3
Contributors
6
Replies
2 Days
Discussion Span
9 Months Ago
Last Updated
7
Views
dwhite12
Newbie Poster
22 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

new.write('\n'.join(keep)).rstrip('\n') (or .lstrip if in beginning) instead of lines 18-23 ?

pyTony
pyMod
Moderator
6,306 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

I had tried something similar to that and obtained the same error i recieved using your suggested line of code.
AttributeError: 'NoneType' object has no attribute 'rstrip'

dwhite12
Newbie Poster
22 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Sorry should be:

new.write('\n'.join(keep).rstrip('\n')) (or .lstrip if in beginning) instead of lines 18-23 ?
pyTony
pyMod
Moderator
6,306 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

For some reason with that line is striping off everything and writing a blank file.

dwhite12
Newbie Poster
22 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Test for length after you strip the line, and note that "new" is already used by Python.

for line in keep:
    if len(line.strip()):
        new_fp.write(line)
woooee
Posting Maven
2,706 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

for line in keep:

if len(line.strip()):

new_fp.write(line)

This unfortually changes the structure of the lines in my file and I need to keep the structure just remove the blank lines before the actually context of the file is wrote.

dwhite12
Newbie Poster
22 posts since Jun 2012
Reputation Points: 0
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.0740 seconds using 2.73MB