2017-06-21 51 views
-1

我無法創建讀取文件,並計算使用這些具體細節該文件的DNA質量的代碼:A = 313.2 C = 289.2 G = 329.2 T = 304.2的Python:讀取文件,並計算質量DNA

所以它是這樣出現的: ACCGAA = 1847.2

此外代碼需要將結果保存在一個新文件中。

該文件僅僅是一個的DNA串在它喜歡.TXT:ACCGTACGT

+3

到目前爲止你有什麼嘗試?你的文件是什麼格式?文件中的數據是什麼樣的? – mtkilic

+0

它只是一個TXT文件,沒有特別的,我真的不知道如何創建新文件並將結果保存在文件中 –

+0

'file = open(「testfile.txt」,「w」)'這會創建testfile.txt爲你'file.write(「Hello World」)''file.close()' – mtkilic

回答

0

我認爲這將讓你開始。 它是什麼,讀dna.txt文件和TXT文件中的文本保存到dna

然後對於每個燒焦它是否等於特定字符,如果是的話,增加的總價值。這是否爲文件中的所有字符。

with open('dna.txt', 'r') as f: 
    dna = f.read() 

total = 0 
for each in dna: 
    if each == "A": 
     total = total + 313.2 
    elif each == "C": 
     total = total + 289.2 
    elif each == "G": 
     total = total + 329.2 
    elif each == "T": 
     total = total + 304.2 

file = open('output.txt', 'w') 
file.write(str(total)) 
file.close() 
+0

這個已經幫了很多,非常感謝! –

+0

讓我知道你是否有任何問題 – mtkilic

0

如果您有興趣從@mtkilic更Python的方式,你可以試試這個:

dna = open('dna.txt', 'r').read() 
d = {'A':313.2, 'C':289.2, 'G':329.2, 'T':304.2} 
total = sum([d.get(each, 0.0) for each in dna]) 
file = open('output.txt', 'w') 
file.write(str(total)) 
file.close() 

我們使用字典存儲每個component.Then的DNA質量,我們評估的總和與列表理解。我們通過get方法訪問關鍵值。如果字典鍵不能識別字符,它將允許它返回0.0質量。