您好,我需要您的幫助解決以下問題。 我解析,看起來像這樣從python數組獲取文件名
@<TRIPOS>MOLECULE
NAME123
line3
line4
line5
line6
@<TRIPOS>MOLECULE
NAME434543
line3
line4
line5
@<TRIPOS>MOLECULE
NAME343566
line3
line4
我currenly有這樣的代碼一個好的工作文件...
mols = []
with open("test2.mol2", mode="r") as molfile:
for line in molfile:
if line.startswith("@<TRIPOS>MOLECULE"):
mols.append(line)
else:
mols[-1] += line
#
for i in range(len(mols)):
out_filename = "file%d.mol2" % i
with open(out_filename, mode="w") as out_file: out_file.write(mols[i]);
但whem我試圖把文件保存名稱根據第二數組的字段,@MOLECULE(NAME ....)之後的代碼,它不起作用。請幫我修復代碼。謝謝!
for i in mols:
out_filename = str(i.split()[1]) + ".mol2" % i
with open(out_filename, mode="w") as out_file: out_file.write(mols[i]);
的錯誤是
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: not all arguments converted during string formatting
但是這隻輸出第一個數組,而不輸出其他數組。 –
而且我還需要@ MOLECULE作爲每個文件的第一行。 –
這應該做到這一點。 – jpaugh