Hi, there

my code is below

resultlines=[targetlines[0] % dResult['SEC'],
                             targetlines[1] % line1b,
                             targetlines[2] % (section_type,dResult['SEC'],mat_data),
                             targetlines[3] % section_data,
                             targetlines[4] % (math.sin(float(dResult['ANG'])*math.pi/180),
                                               math.cos(float(dResult['ANG'])*math.pi/180),
                                               0)]
            for line9 in resultlines:
                   line9=line9+"\n"
                   binf.write(line9)

, which generated the format with empty lines below

*beam section,section=I,elset=UC356X368X202,material=STEEL
0.1873,.3746,.3747,.3747,.027,.027,.0165
1.000000, 0.000000, 0.000000
*element,type=b31,elset=LCHS965-14-3
39,3490,1612

*beam section,section=PIPE,elset=LCHS965-14-3,material=S355
0.4825,.0143
0.000000, 1.000000, 0.000000
*element,type=b31,elset=LCHS965-14-3
40,3492,253

*beam section,section=PIPE,elset=LCHS965-14-3,material=S355
0.4825,.0143
0.000000, 1.000000, 0.000000
*element,type=b31,elset=LCHS965-14-3
41,259,3537

In order to delete these empty lines, I added code below

for line9 in resultlines:
                if line9.isspace():
                    pass
                else:
                   line9=line9+"\n"
                   binf.write(line9)

Unfortunately I got the same format. My code does not work. Could you reminder me of something? Thank you so much.

regards

ning

temp = fopen(tempname,'w')
for line in open(original_name):
if cmp(line,'\n') == 0:
line = line.replace('\n','')
end if
f.write(line)
end for
temp.close()
f.remove()
temp.rename(original_name)

Member Avatar for leegeorg07

your own answer was adding the newlines:

for line in resultlines:
    if line.isspace():
        resultlines.remove(line)
    else:
        pass

that is what is should have been

your own answer was adding the newlines:

for line in resultlines:
    if line.isspace():
        resultlines.remove(line)
    else:
        pass

that is what is should have been

Hi, leegeorg07

Thank you. Your code does not work .

Hi , amitattri1987

It is difficult for me to read two lines in your code

temp = fopen(tempname,'w')
f.write(line)

as I do not understand where "f" comes from

Hi, there

Let me put a relevant part here

targetlines=["*element,type=b31,elset=%s",
                     "%s",
                     "*beam section,section=%s,elset=%s,material=%s",
                     "%s",
                     "%f, %f, %f"]

I print out line9 using the code below

for line9 in resultlines:
                print "line9:", line9
                import time
                time.sleep(2)    
                if line9.isspace():
                    import time
                    time.sleep(5)
                    resultlines.remove(line9)
                else:
                    pass

#                line9=line9+"\n"
                binf.write(line9)

and got
line9: *beam section,section=I,elset=UC356X368X153,material=STEEL
line9: 0.181,.362,.3705,.3705,.0207,.0207,.0123
line9: -0.866025, 0.500000, 0.000000
line9: *element,type=b31,elset=UC356X368X129
line9: 31,472,619

line9: *beam section,section=I,elset=UC356X368X129,material=STEEL
line9: 0.1778,.3556,.3686,.3686,.0175,.0175,.0104
line9: 1.000000, 0.000000, 0.000000
line9: *element,type=b31,elset=UC305X305X118
line9: 32,474,620

line9: *beam section,section=I,elset=UC305X305X118,material=STEEL
line9: 0.15725,.3145,.3074,.3074,.0187,.0187,.012
line9: 1.000000, 0.000000, 0.000000
line9: *element,type=b31,elset=UC254X254X73
line9: 33,658,657

ning

Here's a way to avoid printing the empty line:

for line in my_text:
    line = line.strip()
    if line:
        print line
    # The 'else' condition here is an empty line, which we don't print

Hi, there

I already sorted it by using the code

for line9 in resultlines:
                line9=line9.strip()
                line9=line9+"\n"
                binf.write(line9)

thanks.

ning

Hi, jlm699

Thank you for your help. Using your code results in a continuous line, which can be fixed using "/n". Actaully I do not understand why this phenomenon happens, although it is fixed.

Have a nice weekend.

best wishes

ning

Hi, there

I already sorted it by using the code

for line9 in resultlines:
                line9=line9.strip()
                line9=line9+"\n"
                binf.write(line9)

thanks.

ning

I would suggest a slight modification to your code:

for line9 in resultlines:
        line9=line9.strip()
        if line9:
            line9=line9+"\n"
            binf.write(line9)

In its current form, even if you encounter a blank line, you'll append a newline and then write it to file... I thoght you were trying to remove blank lines? Anyway I'm glad you've found a solution

Hi, there

Could you please explain why this phenomenon happans? Does it associated with the format definition: targetlines I posed above?
Thank you.

regards

ning

Yes it depends on your input data. If a line already has a newline character at the end ('\n') and you append a newline with line9 = line9+'\n' then your resulting data will have two newlines, which will print out as such:

>>> line9 = 'This is a test line\n'
>>> line9 = line9 + '\n'
>>> print line9, 'This is another line'
This is a test line

This is another line
>>>

Hope that helps

Forget previous code. These are step by step instructions of you problem.

Steps to delete blank lines from a file:
1. First find all the characters that generate visible text. Like '\n','\r' and there can be many. Put them in a list BLANK.
2. Create a temporary file: Temp = open(TempName,'w')
3. Open Original file in read mode:
for line in open(OriginalFileName):
for char in line:
if char not in BLANK:
NotBlank = 1
end if
end for
if NotBlank == 0:
line = line.replace(line,'')
end if
Temp.write(line)
end for
Remove Original File : os.remove(OriginalFilepath)
Rename Temp file to original file : os.rename(TempFilePath)

NotBlank is a flag initialized to 0.
'\n','\r' are invisible characters and not visible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.