-1

newbie here!

I have a text file containing fasta headers (about 1.5K; abc.txt):

>CP008746  location=complement(3239792..3241504),organism=Methanosarcina barkeri   CM1,definition=methyl-coenzyme M reductase alpha subunit McrA
>CP009530  location=complement(2979486..2981198),organism=Methanosarcina barkeri 227,definition=Methyl coenzyme M reductase alpha subunit

And I want to remove all besides the first section:

>CP008746
>CP009530

I've been learning Python over the holidays, so I've written (Python3.7.6):

with open("abc.txt","r+") as data_file:
    for line in data_file:
        data=line.split()
        del data[1:]
        print(data)

This gives me the output I want, but I'm not sure how to output the results straight to a new file - I've tried print(data, file=data_file), but it just outputs a few of my lines instead of all. I've gotten around this by copy-pasting manually to a new file but there must be a way to automatically output everything, right?

Any help is much appreciated and I apologise if this has already been answered...!

Thank you!

Steph
  • 9
  • 1
  • That code does **not** give you the output you want. Instead of `>CP008746` it prints `['>CP008746']`. – Kelly Bundy Jan 05 '22 at 17:13
  • @KellyBundy You are right but since I haven't figured how to do what I want without converting my headers to lists, ['>CP008746'] is still an acceptable output for me :D The answers below helped me output everything to a separate file which I can then change to the correct output! – Steph Jan 05 '22 at 17:28

2 Answers2

-1

You can try this. Many solutions are possible, here is one:


with open("abc.txt","r+") as data_file:
    list_of_elements=[] # creating an empty list to store all elements interesting to you
    for line in data_file:
        data=line.split()
        del data[1:]
        print(data)
        list_of_elements.append(str(data)) # appending the list with the elements

with open('file_to_write.txt', 'w') as f: # writing to a file
    for line in list_of_elements:
        f.write(line)
        f.write('\n') # escape line at each element
kelyen
  • 202
  • 1
  • 8
-1

If you are on linux run your program like this:
python3 file.py > output.txt
or write your data to the file from python with:

with open("output.txt", "w") as f:
    f.write(data)
Paul Kocian
  • 487
  • 4
  • 20