我是一个物理系的本科生,唯一的计算机编程经验是我在暑假上的python课,所以我对python的所有东西都很陌生。我正在编写一个python脚本,它将从一个.xyz文件中获取一组坐标,然后将坐标系移动到文件中的一个坐标的中心。也就是说,一个坐标将变为零,所有其他坐标将在x、y和z方向上平移以匹配。你知道吗
我的脚本可以读入文件并将其转换为数组,但当我尝试更改临时数组中的值时,会出现以下错误:
File "E:/Computational Python Scripts/Test.py", line 29, in Carbon_Two_Origin
Temp_Array[i][1] = XYZ_File[i][1] + 0.32101
TypeError: must be str, not float
下面是给我带来问题的整个脚本:
import numpy as np
def file_name():
while True:
file_name_input = input('Type in the name of the xyz file: ')
try:
file = open(file_name_input)
lines = file.readlines()
file.close()
Geometry = []
for line in lines:
Geometry.append(line.split())
print('You have selected', file_name_input)
return Geometry
except IOError:
print('That is not a valide file name. Try again.')
#Defines the variable XYZ_File to be the input file given by user
XYZ_File = tuple(file_name())
#REMOVE THIS LATER
print(XYZ_File)
#Generates a new set of position vectors that has the target carbon, C2, at origin.
def Carbon_Two_Origin():
Temp_Array = np.array(XYZ_File)
for i in range(len(XYZ_File)):
for j in range(len(XYZ_File[i])):
Temp_Array[i][1] = XYZ_File[i][1] + 0.32101
return Temp_Array
New_Geometry = Carbon_Two_Origin()
print(New_Geometry)
就像paulh所说的问题是split return str,所以你有两个选择:在split或sum操作之前将所有内容转换为float。我喜欢分开。你知道吗
相关问题 更多 >
编程相关推荐