2017-11-28 23 views
0

tweepyライブラリを使用してツイートテキストのリストを取得すると、200個のツイートの単語をストップワードのリストと比較し、つぶやきのテキストリストの単語は、検索されたつぶやきの中で最も表示される単語が何であるかを言うことができます。'utf-8'でエンコードされた文字列のリストを通常の文字列に変換する

私がtweet.textsを取得するときは、それを得るためにエンコードしなければならないので、b'wordのリストを取得する必要があります。これは、通常の文字列であるストップワードのリストとは比較できません。

def get_tweetwords(): 
    print("Ingrese el hashtag a buscar, no olvide escribir el numeral (#)(Ctrl+3)") 
    hashtag=str(input()) 
    while (hashtag[0])!="#": 
     print("No olvide escribir el numeral. Vuelva a escribir el hashtag:") 
     hashtag=str(input()) 
    busqueda=tweepy.Cursor(api.search,q=hashtag).items(10) 
    twlist=[] 
    listadepalabras=[] 
    for tweets in busqueda: 
     twlist.append(tweets.text.encode('utf-8')) 
    for i in twlist: 
     x=i.split() 
    for j in x: 
     listadepalabras.append(j) 
    return(listadepalabras) 

私はstopwordsリストと比較し、そのストップワードを除去するために、文字列リストにlistadepalabrasリストをデコードする必要があります。

def get_stopwords(): 
    listastopwords=[item for item in open("stopwords.txt").readlines()] 
    for item in listastopwords: 
     if "\n" in listastopwords: 
      listastopwords[listastopwords.index(item)]=item.replace("\n","") 
    return(listastopwords) 

def sacar_stopwords(): 
    listadepalabras=get_tweetwords() 
    listastopwords=get_stopwords() 
    for i in listadepalabras: 
     for j in listastopwords: 
      if i==j: 
       listadepalabras.remove(j) 
    return(listadepalabras) 

私のテキストリストがb'word'形式での単語が含まれていますし、私のストップワードリストがちょうど'word'

def repeticiones_palabra(): 
    listadepalabras=sacar_stopwords() 
    diccionario=collections.Counter(listadepalabras) 
    diccionario=dict(diccionario.most_common(10)) 
    print ("-LAS 10 PALABRAS MAS UTILIZADAS SON-") 
    print(diccionario) 

である。これは私の私のリストにある10個の最も使用される単語を取得する必要がありますし、それが働いているので、これが動作しません私はストップワードがリストから削除されていないと言うことができるので、私はほとんどのストップワードとハッシュタグを探しています。

repeticiones_palabra() 

私は私のpythonに初心者イム、自分自身を明らかにしたと同様に、スタックオーバーフローしたいと考えています。前もって感謝します。

lst = [b'abc', b'def'] 
# >>> [b'abc', b'def'] 
lst2 = [s.decode("utf-8") for s in lst] 
# >>> ['abc', 'def'] 

EDIT: 私がいることがわかり

+0

Bはバイト文字列ではなく、UTF-8文字列です。 "abcde" .decode( "utf-8") –

答えて

0

b'some文字列は」通常の文字列に変換するには

をバイト文字列され、あなたは内蔵の文字列オブジェクトの機能.decode()を使用する必要があります。以前

for tweets in busqueda: 
    twlist.append(tweets.text.encode('utf-8')) 

は、なぜあなたはエンコードんライン上の文字列をエンコードして、デコードしたいですか?

だけのように変更します。

for tweets in busqueda: 
    twlist.append(tweets.text) 
+0

デコードしないとプログラムが壊れますので、200個のつぶやきを検索する必要があり、エンコードせずに取得できないようです。とにかく何かをやって、リストの項目を一つずつデコードしました!それは今働いているように見えます。 1分で新しいコードを追加しないでください。 –

+0

これは私が書いたものです:lst2 = [sのldのためのs.decode( "utf-8")] –

関連する問題