2017-05-23 51 views
0

我試圖將多個變量傳遞給Tweepy的api.update_status函數。我試圖從光標調用兩個變量,當我這樣做時只會輸出一個變量。將多個變量傳遞給Tweepy api.update_status

for (id, tweet_id, screen_name, created_at, text) in cursor: 
url='https://www.threatminer.org/host.php' 
payload={'q': text, 'api': 'True', 'rt': '6'} 
headers={ 
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'} 
r=requests.get(url, params=payload, headers=headers) 
json_data=r.json() 
status_message=json_data['status_message'] 
status = text and "Was found in a Tweet" 
if "Results found." in status_message: 
    api=tweepy.API(auth) 
    api.update_status(text, screen_name) 
    print(text, screen_name) 
    print(r.url) 
    print json_data 
else: 
    print('Nothing') 
+0

你是什麼意思「傳遞多個變量到api.update_status」?你有兩個或更多的變量包含你想發送的文本? –

+0

@RodrigoLeite這是正確的。我有多個包含我想要發送的文本的變量。 –

+0

我提交了一個答案 –

回答

1

正如您的評論所述,您有多個變量包含您要發送的文本。爲了解決這個問題,你需要連接它們來形成一個單一的字符串。

如果你正在使用Python 3.6+您在使用格式的字符串,像這樣的選擇:

string1 = "My name is" 
string2 = "dog!" 
string3 = f"{string1} {string2}" 

在這種情況下,string3將等於My name is dog!。但是,如果你不上的Python 3.6或更高版本,你需要使用format()功能做到這一點,像這樣:

string1 = "My name is" 
string2 = "dog!" 
string3 = "{} {}".format(string1, string2) 

另一種選擇是使用+運營商,但是這在性能方面的原因皺起眉頭。

+0

這工作,感謝您的幫助。 –