2014-07-26 3 views
-2
import re, codecs 
import string 
import sys 
stopwords=codecs.open('stopwords_harkat1.txt','r','utf_8') 
lines=codecs.open('Corpus_v2.txt','r','utf_8') 
for line in lines: 
    line = line.rstrip().lstrip() 
    #print line 
    tokens = line.split('\t') 
    token=tokens[4] 

    if token in stopwords: 
      print token 

이 코드에는 오류가 없지만 다른 파일의 문자열 일치는 작동하지 않습니다. 나를 도와주세요. 유니 코드 텍스트 파일에서 문자열이 일치합니까? python

은 $ 나는 또한 방법 매치를 시도했지만

에만 열지, 컨텐츠 파일을로드 할 필요

답변

0

작동하지 않습니다.

stopwords = codecs.open('stopwords_harkat1.txt','r','utf_8') 

에 : 나는 그것을 시도

with codecs.open('stopwords_harkat1.txt','r','utf_8') as f: 
    # assuming one stop word in one line. 
    stopwords = set(line.strip() for line in f) 

    # Otherwise, use the following line 
    # stopwords = set(word for line in f for word in line.split()) 
+0

하지만이 오류 현재 :

행 다음에 교체 역 추적 (가장 최근에 전화를 마지막) : 파일 "C를 : 바탕 화면 \ 사용자 \ 것은 \ 제거 (codecs.open ('stopwords_harkat1.txt', 'r', 'utf_8'))을 f : 으로 열어 의 단어 'remove words \ remove \ remove.py "에 입력하십시오. TypeError : 유니 코드로 강제 변환 : need string 또는 버퍼 발견, 인스턴스 – msm

+0

@ msm, 선두의'open ('오타가되었습니다. 나는 대답을 업데이트했다. 그것을 확인하시기 바랍니다. – falsetru

관련 문제