1

I'd like to export the list of sequences obtained in this program to a txt file. How can I do that?

with open("example_val.txt") as f:
  sequence = []
  for line in f:
      if line.startswith(""):
            line = line[:-1]
            seq = (line.split("<|endoftext|>")[1].split(':')[0] + ":")
            output.write(seq)
            sequence.append(seq)
            print(seq)

output:

ERRDLLRFKH:
RRDLLRFKHG:
RDLLRFKHGD:
DLLRFKHGDS:
LLRFKHGDSE:
LSPATRWGMI:
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
  • Since you already have a loop to make the list, you could just add a couple lines to the code you already have: 1. Open the file to write at the same time you open the file to read: `with open("example_val.txt") as f, open("output.txt", "w") as wf:` and 2. Add the `wf.write()` call inside the loop for each `seq` – Pranav Hosangadi Jul 28 '22 at 13:25

1 Answers1

2

Given the sequence is in strings, just do

sequence = [i+"\n" for i in sequence]
with open("file.txt", "w+") as f:
  f.writelines(sequence)
doa4321
  • 176
  • 2
  • 9