Chip
Chip

Reputation: 471

searching file using re.search and saving output to new file

I have been wrestling with this for some time now:

I am searching a file for a pattern and want to save the output to another file. However I keep getting the error:

'str' object has no attribute 'write' 

Here is the code to grab the file on Google Colab:

!wget 'ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.dat.gz'

!gunzip uniprot_sprot.dat.gz

This is what I have tried:

import re

with open('uniprot_sprot.dat', 'r') as f:
  for i in f:
    #if re.search(r"^ID|^SQ|^\s", i):
    if re.search(r'^ID|^\s', i):
      print(i, file = 'output.txt')

I can print to standard output just fine, however when writing to file the above error message occurs.

The desired output will be a file containing the following (with many more lines):

ID   ACDH3_MYCVP             Reviewed;         306 AA.

 MADKKSVAIV GSGNISTDLL YKLLRSEWLE PRWMIGIDPE SEGLARARKL GLETSHEGVD

 WLLAQSELPD MVFEATSAYV HKAAAPRYAE AGIRAIDLTP AAVGPGVIPP ANLRAHLDAP

 NVNMVTCGGQ ATIPMVYAVS RVVEVPYAEI VASVSSASAG PGTRANIDEF TKTTSAGVQN

 IGGAQRGKAI IILNPAEPPM IMRDTIFCAI PEHADHAAIT QSIKDVVAEV QTYVPGYRLL

 NEPQFDEPSV VNGGNHVVTV FVEVEGAGDY LPPYAGNLDI MTAAATKVGE EIAKESLAAT

 AGGAQA

ID   ACDH3_NOCFA             Reviewed;         328 AA.

 MNHMSDPTTV KVAVIGSGNI GTDLMIKVIR ...

Thanks

Upvotes: 0

Views: 245

Answers (2)

azro
azro

Reputation: 54148

You may open the output file, the same you did for the input one

with open('uniprot_sprot.dat') as f_in, open('output.txt', 'w') as f_out:
    for i in f_in:
        if re.search(r'^ID|^\s', i):
            f_out.write(i)

Upvotes: 1

Daweo
Daweo

Reputation: 36570

This

print(i, file = 'output.txt')

is not correct usage, file should be a file-like object (stream); defaults to the current sys.stdout. example usage:

i = [1,2,3]
with open("output.txt", "w") as f:
    print(i, file=f)

Upvotes: 0

Related Questions