file→read molecule→pdb文件
出现以下代码报错

关注让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如需一对一服务私聊我哦这段代码出现报错的原因可能是没有正确调用文件读取函数,或者文件路径错误。 以下是可能的修改方案:
with open('example.pdb', 'r') as f:
content = f.read()
with open('example.pdb', 'r', encoding='utf-8') as f:
content = f.read()
import argparse
def read_pdb(filename):
coordinates = []
with open(filename, 'r') as f:
for line in f:
if line.startswith('ATOM'):
x, y, z = float(line[30:38]), float(line[38:46]), float(line[46:54])
coordinates.append((x, y, z))
return coordinates
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Read atomic coordinates from PDB file')
parser.add_argument('-f', '--file', type=str, required=True, help='PDB file to read')
args = parser.parse_args()
filename = args.file
coordinates = read_pdb(filename)
print(f'Read {len(coordinates)} atomic coordinates from {filename}')