0

Hey I want to arrange a text file using python. My python code is working perfectly but it's not give me expected answer. Let me explain I have a text file same like this:-

serial
name
phone 
gmail
1
blah blah
55555
blah@blah.com

I wrote my script like this :-

out=open('out.txt','w')  
with open('blah.txt') as b:
    i=1
    for line in b:
        if i==1:
            out.write(line)
            i=i+1
        elif type(line)==int:
            out.write('\n'+line)
        elif type(line)==str:
            out.write('\b\t'+line)
        else:
            pass
out.close()

I didn't wrote entire program but it's like this. But it's give me my output same as my input. Am I missing something?

My expected Ans is:-

serial    name      phone   gmail
1        blah blah   55555   blah@blah.com

3 Answers3

1

You are trying to transform lines of text to columns. This code is supposing that in your file blah.txt you are having same amount of headers and values:

with open('blah.txt', 'r') as f_in, open('out.txt','w',newline='') as f_out:
    lines = [l.strip() for l in f_in.readlines()]
    headers, values = lines[:len(lines)//2], lines[len(lines)//2:]
    for h in headers:
        f_out.write(h + '\t\t')
    f_out.write('\n')
    for v in values:
        f_out.write(v + '\t\t')
    f_out.write('\n')

With this the out.txt will be:

serial      name        phone       gmail       
1       blah blah       55555       blah@blah.com   
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Can you please explain me what \\2 does? – Anirban Singha Aug 01 '18 at 17:56
  • @AnirbanSingha It's called integer division - here in this question it's properly explained https://stackoverflow.com/questions/183853/in-python-2-what-is-the-difference-between-and-when-used-for-division – Andrej Kesely Aug 01 '18 at 17:58
1

You can use str.center() for alignment, and need to add a \n after half the lines are written:

Create test file for minimal verifyable complete example:

text ="""serial
name
phone 
gmail
1
blah blah
55555
blah@blah.com"""

fn = "t.txt"

with open(fn,"w") as f:
    f.write(text)

Process file:

fn = "t.txt"

lines = []
with open(fn,"r") as f:
    lines = [x.strip() for x in f.readlines()]

# what is the longest data  items? space others accordingly:
longest = max(len(x) for x in lines)

with open("t2.txt","w") as f: 
    # write first half of rows
    for header in lines[:(len(lines)//2)]:
        f.write( str.center( header, longest+2))
    f.write("\n")
    # write second half of rows
    for data in lines[len(lines)//2:]:
        f.write( str.center( data, longest+2))
    f.write("\n")

Read back in and output for verification:

print("\n")
with open("t2.txt","r") as r:
    print(r.read())

Output:

   serial          name          phone          gmail     
     1          blah blah        55555      blah@blah.com 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-2

You aren't closing the output file:

Try adding out.close() at the end.

Gigaflop
  • 390
  • 1
  • 13