2016-10-25 2 views
-2

텍스트 마이닝 프로세스를위한 단어를 줄이려고합니다. 나는 CSV 파일을 가지고있다. 내 텍스트 데이터가 열 이름 'Abstrac'에 수집되었습니다. 그래서 다음 코드를 실행하려고하지만 오류 'file' object has no attribute '__getitem__'이 나타납니다.파이썬에서 토큰 화에 '__getitem__'오류가 발생했습니다.

enter image description here

def get_tokens(): 
    with open('scopus (1).csv', 'r') as data: 
     text = data['Abstract'].read() 
     lowers = text.lower() 
    #remove the punctuation using the character deletion step of translate 
     no_punctuation = lowers.translate(None, string.punctuation) 
     tokens = nltk.word_tokenize(no_punctuation) 
    return tokens 
tokens = get_tokens() 
count = Counter(tokens) 
print count.most_common(10) 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-12-e7af75d6fd69> in <module>() 
     7   tokens = nltk.word_tokenize(no_punctuation) 
     8  return tokens 
----> 9 tokens = get_tokens() 

<ipython-input-12-e7af75d6fd69> in get_tokens() 
     1 def get_tokens(): 
     2  with open('scopus (1).csv', 'r') as data: 
----> 3   text = data['Abstract'][i].read() 
     4   lowers = text.lower() 
     5  #remove the punctuation using the character deletion step of translate 

TypeError: 'file' object has no attribute '__getitem__' 
+0

을'data' 그냥 파일 - 류의 객체가 아닌 파일의 내용을 구문 분석에 의해 생성 된 데이터 구조입니다. – chepner

답변

1
text = data['Abstract'][i].read() 

당신은 단지 Abstract 열에서 데이터를 읽을하려고합니까? 그렇다면, csv.DictReader 클래스 사용

r = csv.DictReader(data) 
for row in r: 
    text = row['Abstract'] 
    #Process line by line 
관련 문제