Youssef S. Youssef S. - 11 months ago 38
Python Question

Flask unsupported operand type(s) for +: 'dict' and 'str'

I tried to implement this code into flask, it's working correctly in the console but with http requests I got this obscure error:

def results():

keyword = {'keyword': request.args.get('keyword')}


for tweet in tweepy.Cursor(,q=keyword+
" -filter:retweets",
raw = tweet.text
clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", raw)
result = cool.api(clean)
return render_template('pages/results.html')

My html form looks like this:

<form action="/results">
<input type="text" name="keyword">
<input type="submit" value="Submit">

I also tried a different method:

kwargs = {
'keyword': request.form['keyword'],

methods=['POST', 'GET']
do not fix the problem.

In the error page, the problem is in this line:

" -filter:retweets",

Answer Source

As the error says, you're trying to add a dictionary to a string:

tweepy.Cursor(,q=keyword+ " -filter:retweets",

keyword is a dictionary. Because Python leans towards strong typing, the conversion to string isn't automatic like it is in some languages. Cast it to a string first:

tweepy.Cursor(,q=str(keyword) + " -filter:retweets",

As the commentor mentioned, Python's error messages are usually quite clear. Make sure you really read them over, as they're often self-explanatory.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download