OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Padding to a specific level and adding character at the end

  • Thread starter Thread starter Patrick Perea
  • Start date Start date
P

Patrick Perea

Guest
I have a text file:

Code:
a
aaa
aaa
a

My desired outcome is:

Code:
a                               N
aaa                             N
aaa                             N
a                               N

I wrote the following:

Code:
newf=""
with open('testfile.txt','r') as f:
   for line in f:
     if len(line) < 87:
         newf+=line.strip()+" "
     else:
        newf+=line.strip()+"N\n"
   f.close()
with open('testfile.txt','w') as f:
  f.write(newf)
  f.close()

When I run this, my outcome becomes:

a aaa aaa a

I can't figure out why it changes the output to one line.
<p>I have a text file:</p>
<pre><code>a
aaa
aaa
a
</code></pre>
<p>My desired outcome is:</p>
<pre><code>a N
aaa N
aaa N
a N
</code></pre>
<p>I wrote the following:</p>
<pre><code>newf=""
with open('testfile.txt','r') as f:
for line in f:
if len(line) < 87:
newf+=line.strip()+" "
else:
newf+=line.strip()+"N\n"
f.close()
with open('testfile.txt','w') as f:
f.write(newf)
f.close()
</code></pre>
<p>When I run this, my outcome becomes:</p>
<p><code>a aaa aaa a</code></p>
<p>I can't figure out why it changes the output to one line.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top