prog
prog

Reputation: 15

How to add tab charcter in each line?

I have a file like this

237501.jpg#0 Two 
237501.jpg#1 Teddy
237501.jpg#2 Large 
237501.jpg#3 A teddy 
237501.jpg#4 an image 
237501.jpg#0 Two 
237501.jpg#1 Teddy
237501.jpg#2 Large 
237501.jpg#3 A teddy 
237501.jpg#4 an image  

and i need to add tab character after each number for each line to be like this

237501.jpg#0     Two 
237501.jpg#1     Teddy
237501.jpg#2     Large 
237501.jpg#3     A teddy 
237501.jpg#4     an image 

my code is

import os

inputFile = open("output1.txt", "r") 
exportFile = open("output10.txt", "w")
for line in inputFile:
   new_line = line.replace("#0", '#0    ')
   exportFile.write(new_line) 

but couldn't catch the number at each line the result i got is

237501.jpg#0     Two 
237501.jpg#1 Teddy
237501.jpg#2 Large 
237501.jpg#3 A teddy 
237501.jpg#4 an image 

Upvotes: 1

Views: 688

Answers (3)

leaf_soba
leaf_soba

Reputation: 2518

you can use enumerate

input.txt:

237501.jpg#0 Two 
237501.jpg#1 Teddy
237501.jpg#2 Large 
237501.jpg#3 A teddy 
237501.jpg#4 an image 
237501.jpg#0 Two 
237501.jpg#1 Teddy
237501.jpg#2 Large 
237501.jpg#3 A teddy 
237501.jpg#4 an image  

code:

inputFile = open("input.txt", "r")
exportFile = open("output.txt", "w")
patterns = ["#"+str(idx) for idx in range(5)]
for line in inputFile:
    for pattern in patterns:
        if pattern in line:    
            new_line = line.replace(pattern, pattern+' '*3)
            exportFile.write(new_line)
            break
inputFile.close()
exportFile.close()

output.txt result:

237501.jpg#0    Two 
237501.jpg#1    Teddy
237501.jpg#2    Large 
237501.jpg#3    A teddy 
237501.jpg#4    an image 
237501.jpg#0    Two 
237501.jpg#1    Teddy
237501.jpg#2    Large 
237501.jpg#3    A teddy 
237501.jpg#4    an image  

Upvotes: 2

Ben Y
Ben Y

Reputation: 1023

You hadn't really specified what kinds of data could be in the first column, but it appears that only the first space is significant. I would partition each space and just print out the pieces, skipping the middle piece.

with open('input.txt') as fd:
    for line in fd:
         parts = line.partition(' ')
         print('\t'.join(parts[0:3:2]), end='')

Upvotes: 0

ThePyGuy
ThePyGuy

Reputation: 18426

You can use regex, iterate through each line, find substring with #1, #2,.. for each line and replace it with the substring + '\t'

import re
op = ''
pattern = re.compile('#\d+')
for line in file:
    num = pattern.findall(line)[0]
    op += line.replace(num, num+ '\t')

print(op)

OUTPUT:

237501.jpg#0     Two 
237501.jpg#1     Teddy
237501.jpg#2     Large 
237501.jpg#3     A teddy 
237501.jpg#4     an image 

Upvotes: 0

Related Questions