0

consider the tab-separated file foo.txt:

chrY    1208806 1208908 +   .
chrY    1212556 1212620 +   .
chrY    1465479 1466558 +   .

The goal is to manipulate foo.txt to obtain result.txt as such:

chrY:1208806-1208908
chrY:1212556-1212620
chrY:1465479-1466558

What I can achieve is only joining by one specific separator not two different ones --> my code:

with open(filename,'r') as f:
    for line in f:
        l = line.split()[0:3]
        result = ':'.join(l)
        print(result)

My outcome:

chrY:1208806:1208908
chrY:1212556:1212620
chrY:1465479:1466558
moth
  • 1,833
  • 12
  • 29

2 Answers2

1

This works

with open(filename,'r') as f:
    for line in f:
        l = line.split()[0:3]
        result = f'{l[0]}:{l[1]}-{l[2]}'
        print(result)
the__hat_guy
  • 133
  • 8
1

Here is a solution you can try out,

with open(file_name, "r") as f:
    for l in f.readlines():
        print("{0}:{1}-{2}".format(*l.split()[:3]))

chrY:1208806-1208908
chrY:1212556-1212620
chrY:1465479-1466558
sushanth
  • 8,275
  • 3
  • 17
  • 28