2017-04-11 135 views
0

我需要從列表中的這個條目中刪除[',\ n \ xa0字符和年份(1994),然後遍歷列表中的每個條目。 有沒有辦法做到這一點?我新望對Python和一直試圖小時從列表中刪除某些字符

的條目是像這樣:

[['The Shawshank Redemption\n(1994)\n\n\n 9.2\xa0\xa0\n\n'], ['The Godfather\n(1972)\n\n\n 9.2\xa0\xa0\n\n'], ['The Godfather: Part II\n(1974)\n\n\n 9.0\xa0\xa0\n\n'], 

編輯:對不起,我不包括代碼,IV設法剝離數字和\ n換行字符在一年之後。但在電影片名之後仍然會出現換行符。生病貼上我的代碼anwyway感謝!:

from bs4 import BeautifulSoup 
import requests 
import random 

names = [] 
newList = [] 
url = 'http://m.imdb.com/chart/top' 
# get contents from url 
content = requests.get(url).content 
# get soup 
soup = BeautifulSoup(content,'lxml') # choose lxml parser 
# find all the references 
ref_tags = soup.findAll('span', { 'class' : 'media-body' }) 
realTags = soup.find_all("h4") 
# iterate through the ResultSet 
for i,ref_tag in enumerate(ref_tags): 
    # print text only 
    names.append('[{0}] {1}'.format(i,ref_tag.text)) 
pos = 0 
for name in names: 
    newName = names[pos] 
    newName = newName[9:] 
    newName = newName[:100] 
    newName = newName.split("(") 
    newName = newName[::2] 
    del newName[2:9:3] 
    newList.append(newName) 
    pos = pos + 1 

print(newList) 
choice = random.choice(newList) 
print(choice) 

輸出是這樣的:

[ '肖申克的救贖\ n'],[ '教父\ n'],['教父:第二部分\ n'],['黑暗騎士\ n'],['12 Angry Men \ n']

+2

你嘗試過什麼究竟,沒有工作? – nbro

+0

正如@nbro提到你應該添加一個[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – geostocker

回答

0

所以我得到它輸出我想要的。謝謝!

繼承人的人誰可能需要在未來的代碼:

from bs4 import BeautifulSoup 
import requests 
import random 

names = [] 
newList = [] 
url = 'http://m.imdb.com/chart/top' 
# get contents from url 
content = requests.get(url).content 
# get soup 
soup = BeautifulSoup(content,'lxml') # choose lxml parser 
# find all the references 
ref_tags = soup.findAll('span', { 'class' : 'media-body' }) 
realTags = soup.find_all("h4") 
# iterate through the ResultSet 
for i,ref_tag in enumerate(ref_tags): 
    # print text only 
    names.append('[{0}] {1}'.format(i,ref_tag.text)) 
pos = 0 
for name in names: 
    newName = names[pos] 
    newName = newName[9:] 
    newName = newName[:100] 
    newName = newName.split("(") 
    newName = newName[::2] 
    del newName[2:9:3] 
    newList.append(newName) 
    pos = pos + 1 

wordChoice = random.choice(newList) 
str = str(wordChoice) 
editWord = str.split("\\n") 



print(editWord[1]) 

和輸出就像這樣:

Shutter Island