0

ツイート (Twitter) のリストを受け取るプロジェクトを作成してから、dictionary特定の値の単語を含む 内に単語があるかどうかを確認します。単語を取得するコードを取得しましたが、次のような記号を削除する方法がわかりません: , . ":

コードは次のとおりです。

def getTweet(tweet, dictionary):
score = 0
seperate = tweet.split(' ')
print seperate
print "------"    
if(len(tweet) > 0):
    for item in seperate:
        if item in dictionary:
            print item
            score = score + int(dictionary[item])
    print "here's the score: " + str(score)
    return score
else:
    print "you haven't tweeted a tweet"
    return 0

チェックされるパラメーター/ツイートは次のとおりです。

getTweet("you are the best loyal friendly happy cool nice", scoresDict)

何か案は?

4

2 に答える 2

0

分割を行う前に、文字をスペースに置き換えてから、スペースで分割します。

import re

line = '  a.,b"c'
line = re.sub('[,."]', ' ', line)

print line  # '  a  b c'
于 2013-08-28T04:38:51.770 に答える