2016-05-14 25 views
-3

我提出我的問題。我有一個苯分子,每個原子都有一個笛卡爾座標。我想要的是旋轉分子圍繞質心在z和x方向。Python簡單的數據工作

這樣做,我需要計算的質量M.

中心在文件中,該分子幾何座標如下(txt文件)給出。

  X Y Z 
atom  4 5 7 C 
atom  3 7 9 C 

我有更多這樣的行。測試文件不僅包含數字和包含數據。

我現在的問題是,我如何在Python中對行和字符串進行總結/總結?因爲對於質量中心,我需要總結所有原子的X座標,然後將X座標除以6.並且我不知道如何做到這一點。

我知道如何選擇某些行和行,但我不需要某些元素。我需要我的程序來讀取行,分割它們,然後選擇特定的sata並對它們進行總結。

請引導我完成接下來的步驟。

在此先感謝。

+4

你有沒有編寫任何代碼來嘗試解決這個問題?您應該將您的代碼添加到問題中。 –

回答

0

我已經寫了兩種實現代碼的方法,這些方法是基於我已經做出的假設並理解這裏需要做的事情。

方法1:計劃1

xcoordinatedata=[] 
fileopen=open("atomdata.txt", "r+") 
i=0 
for rows in fileopen.readlines(): 
    if i==0: 
     i=1 
    pass 
else: 
    xcoordinatedata.append(float(rows.split("\t")[1])/6) 

fileopen.close() 
print "The X coordinate data is", xcoordinatedata 

方法2:第二個方法實現相同的程序

xcoordinatedata=[] 
fileopen=open("atomdata.txt", "r+") 
i=0 
for row in fileopen.readlines(): 
    if i!=0: 
     xcoordinatedata.append(float(row.split("\t")[1])/6) 
     i+=1 
    i=1 

fileopen.close() 
print "out of the program", xcoordinatedata 

請讓我知道,如果這有助於。請提供您的代碼,以便我可以進一步幫助您。