mass_dictionary = {'C':12.0107,
'O':15.999
#Others...?
}
# If your files are this structured, you can just
# hardcode some column assumptions.
coords_idxs = [6,7,8]
type_idx = 9
# Open file, get lines, close file.
# Probably prudent to add try-except here for bad file names.
f_open = open("Test.txt",'r')
lines = f_open.readlines()
f_open.close()
# Initialize an array to hold needed intermediate data.
output_coms = []; total_mass = 0.0;
# Loop through the lines of the file.
for line in lines:
# Split the line on white space.
line_stuff = line.split()
# If the line is empty or fails to start with 'ATOM', skip it.
if (not line_stuff) or (not line_stuff[0]=='ATOM'):
pass
# Otherwise, append the mass-weighted coordinates to a list and increment total mass.
else:
output_coms.append([mass_dictionary[line_stuff[type_idx]]*float(line_stuff[i]) for i in coords_idxs])
total_mass = total_mass + mass_dictionary[line_stuff[type_idx]]
# After getting all the data, finish off the averages.
avg_x, avg_y, avg_z = tuple(map(lambda x: (1.0/total_mass)*sum(x), [[elem[i] for elem in output_coms] for i in [0,1,2]]))
# A lot of this will be better with NumPy arrays if you'll be using this often or on
# larger files. Python Pandas might be an even better option if you want to just
# store the file data and play with it in Python.
來源
2012-07-27 15:48:25
ely
首先,這是一個功課嗎? – FallenAngel 2012-07-27 15:30:39
歡迎來到SO!你能向我們展示迄今爲止的代碼嗎?如果你有讀取文件的代碼並獲得'x'值作爲字符串,那麼這是一個很好的開始!基本上,如果你告訴我們你有什麼,我們可以幫助你改進它,並讓它達到你可以使用它的地步。 – mgilson 2012-07-27 15:30:43
這是從您的軟件作爲製表符分隔文件?如果是這樣,你可以看看http://docs.python.org/library/csv.html – Jzl5325 2012-07-27 15:30:49