2014-04-28 5 views
-1

저는 초보자이며 비단뱀입니다.이 오류가 붙어 있습니다. 내가 잘못 된 곳을 말해 준다면 큰 도움이 될 것입니다.Python : ValueError : 2가 목록에 없습니다.

내가 같은 출력을 얻고 그러나
import math 
import nltk 
import json 
import pymongo 
#import sys 
import nltk.corpus 

from funcs import get_name_entity 
from funcs import sentiment_analysis 
from funcs import get_wordlist 
from funcs import get_pos_tag 

def get_post(count): 
    data =() 
    client = pymongo.MongoClient("localhost", 27017)  #take data from database Mongo 
    db = client.News_Updated        #accessing the database News_Updated 
    db.News_Updated.find() 
    temp = [] 
    #print count 
    if count == 0: 
     data = db.ndtv 
    elif count == 1 : 
     data = db.timesofindia 
    elif count == 2 : 
     data = db.hindustantimes 
    elif count == 3 : 
     data = db.asianetnews 
    elif count == 4 : 
     data = db.khabarndtv 
    #print data.find() 

    for post in data.find(): 
     temp.append(post) 
    return temp 

if __name__ == '__main__': 
    count = 0 
    limit = -1 
    while count < 5: 
     data = get_post(count) 
     count += 1 
     for text in data.index(2): 
      print text   

:

Traceback (most recent call last): File "main.py", line 43, in for text in data.index(2): ValueError: 2 is not in list

여기 MongoDB를 데이터의 샘플입니다 여기 내 코드입니다.

{u'url': u'62-year-old-woman-murdered-in-tamil-nadu-s-salem-510469', u'_id': ObjectId('53551ebf39b71c444a991341'), u'data': u"Salem, Tamil Nadu:In a daring daylight murder, a 62-year-old woman was killed by two youths for a gold chain in a busy commercial locality in Salem, Tamil Nadu, police said.Dhanlakshmi was behind the counter of her husband's small shop on Arunachalaachari street in his absence when the assailants slit her throat with a knife and fled away taking the gold chain she was wearing, they said.Arunachalaachari street, dotted with jewellery, vessels and textile shops, is considered a busy commercial locality of the district. The incident sent shock waves among the locals.Deputy Commissioner of Police A G Babu visited the spot."}

도움을 주시면 대단히 감사하겠습니다.

+0

파이썬 초보자를위한 중요한 도구 (또는 그 이상 - 초급 ...) : "import pdb; pdb.set_trace()". 로컬 변수의 이름을 디버거에 입력하여 포함해야한다고 생각하는 것을 포함하지 않는 것을 찾으십시오. –

답변

1

in the docs을 지정하면 index()은 지정된 요소의 목록 색인을 검색합니다. 귀하의 경우는 2입니다. 귀하의 순서 (data)에 2 같은 요소가 없기 때문에 나타나는 오류입니다.

아마 data[2]을 사용하고 싶습니까? 그것은 당신의 질문에서 아주 명확하지 않습니다.

관련 문제