我試圖在xyz文件中讀入python,但不斷收到這些錯誤消息。有點新的Python,所以會喜歡一些幫助解釋它!讀取具有可變列的文件
def main():
atoms = []
coordinates = []
name = input("Enter filename: ")
xyz = open(name, 'r')
n_atoms = xyz.readline()
title = xyz.readline()
for line in xyz:
atom, x, y, z = line.split()
atoms.append(atom)
coordinates.append([float(x), float(y), float(z)])
xyz.close()
return atoms, coordinates
if __name__ == '__main__':
main()
Error:
Traceback (most recent call last):
File "Project1.py", line 25, in <module>
main()
File "Project1.py", line 16, in main
atom, x, y, z = line.split()
ValueError: not enough values to unpack (expected 4, got 3)
我認爲值誤差是因爲幾行後,只有3個值。但不知道爲什麼我得到返回錯誤。
非常感謝!我一定會在未來考察圖書館! – Camerann