2016-11-01 65 views
0

我想創建一個列表,其中每個元素都將成爲聯機文件的一行。但是當我想分割我的列表時,我收到EM:「TypeError:需要類似字節的對象,而不是'str'」。我已經嘗試解決這個問題('utf8'),但我不能。任何建議?拆分一個列表:類型錯誤:需要類似字節的對象,而不是'str'

def collect_record(name): 
file = "http://www.uniprot.org/uniprot/%s.txt" %name 
u=urllib.request.urlopen(file) 
pdblines=u.readlines() 
for line in pdblines : 
    line = ligne.strip() 
    pdblines = line.split("b") 
u.close() 
return pdblines 
+0

你需要完整回溯添加到您的帖子。此外,您的代碼中存在拼寫錯誤。 'ligne.strip()'。請提供[mcve]。 –

回答

0

因爲它是一個文本文件,所以你不必特別做任何事情。

def collect_record(name): 
    file = "http://www.uniprot.org/uniprot/%s.txt" % name 
    lines = [i for i in urllib.request.urlopen(file)] 
    return lines 

如果你想更明確的

def collect_record(name): 
    file = "http://www.uniprot.org/uniprot/%s.txt" % name 
    lines = str(urllib.request.urlopen(file).read()).split('\n') 
    return lines 
+0

謝謝你的幫助! –

相關問題