I am trying to store three different variables(which are results from a for loop) as one line in a file. My entire code just in case you wondering what I am trying to do :
from Bio.PDB import *
from Bio import SeqIO
from glob import glob
parser = PDBParser(PERMISSIVE=True)
pdb_files = glob('/pdb_RF0001/*')
for fileName in pdb_files:
structure_id = fileName.rsplit('/', 1)[1][:-4]
structure = parser.get_structure(structure_id, fileName)
model = structure[0]
for residue1 in structure.get_residues():
for residue2 in structure.get_residues():
if residue1 != residue2:
try:
distance = residue1['P'] - residue2['P']
except KeyError:
continue
f = open('%s.txt' % fileName, 'w')
line = str(residue1)+','+str(residue2)+','+str(distance)
f.write(line)
f.close()
break
Sample code for check :
f = open('%s.txt' % fileName, 'wb')
line = int(residue1)+','+int(residue)+','+float(distance)
f.write(line)
f.close()
How to store the three different variables from the line variable as one line in an output file?
使用f-string
line = f"{residue1}, {residue}, {distance}"
int(residue)
is an integer, and float(distance)
is a real number (specifically, a floating-point number, hence the " float
"). Thus, in this line, you are trying to add numbers to strings:
line = int(residue1)+','+int(residue)+','+float(distance)
However, Python disallows this. What you probably want to do is convert residue1
, residue
, and distance
from (what I assume are) numbers to strings, like this:
line = str(residue1)+','+str(residue)+','+str(distance)
str.format()是 Python 中的字符串格式化方法之一
"{}, {}, {}".format("residue1", "residue", "distance")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.