gpt4 book ai didi

Python:ValueError:混合迭代和读取方法会丢失数据

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:21 24 4
gpt4 key购买 nike

当执行下面的脚本时,我得到:

ValueError:混合迭代和读取方法会丢失数据

我知道这是因为 num 在第一个 for 循环内,而 last_host 是 num 的依赖项,但我不知道如何解决这个问题。

#!/usr/bin/env python2
import datetime as dt
import glob as glob
import os as os
import shutil as shutil
import signal as signal
import subprocess as sp
import sys as sys

# Open PDB file and read coordinates
pdb_file = open('align_z.pdb', 'r')
new_pdb_file = open('vac.pdb', 'w')



#Get last host atom
for num, line in enumerate(pdb_file, 1):
if "L01" in line:
print num
break


last_host=int(num)
print(last_host-1)

for atom in range(0, last_host-1):
data = pdb_file.readline()
new_pdb_file.write(data)

最佳答案

一旦通过 enumerate 迭代 pdf_file 就不能再次迭代它,除非调用 pdb_file.seek(0) seek(0) 改变流位置到开头

这是我的修改:

num = 1
for line in pdb_file:
num += 1
if "L01" in line:
print num
break

pdb_file.seek(0) # go back to the beginning and then it can be iterated again

last_host=int(num)
print(last_host-1)

for atom in range(0, last_host-1):
data = pdb_file.readline()
new_pdb_file.write(data)

关于Python:ValueError:混合迭代和读取方法会丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42423210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com