Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对tweepy多次使用拆分函数会导致索引器错误:列表索引超出范围_Python_Twitter_Tweepy - Fatal编程技术网

Python 对tweepy多次使用拆分函数会导致索引器错误:列表索引超出范围

Python 对tweepy多次使用拆分函数会导致索引器错误:列表索引超出范围,python,twitter,tweepy,Python,Twitter,Tweepy,我正在使用split函数,它在data上失败,并给出错误信息: IndexError: list index out of range 数据等于: 1457410676.51::RT @robbwolf: Earthquake in Reno! Wacky. 1457410777.98::13:19 #\u5730\u9707 #earthquake https:\/\/t.co\/2X8FaipZsW 1457410814.04::1.7 magnitude #earthquake. 12

我正在使用
split
函数,它在
data
上失败,并给出错误信息:

 IndexError: list index out of range
数据等于:

1457410676.51::RT @robbwolf: Earthquake in Reno! Wacky. 1457410777.98::13:19 #\u5730\u9707 #earthquake https:\/\/t.co\/2X8FaipZsW 1457410814.04::1.7 magnitude #earthquake. 12 mi from Anza, #CA, United States https:\/\/t.co\/1GWexXmLom 1457410819.04::1.7 magnitude #earthquake. 12 mi from #Anza, CA, United States https:\/\/t.co\/fL5MDx7bhS
代码:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time

ckey = ''
csecret = ''
atoken = ''
asecret = ''

class Listener(StreamListener):

    def on_data(self, data):
        try:
            tweet = data.split(',"text":"')[1].split('","source')[0]
            location = data.split(',"location":"')[1].split('","url')[0]
            saveThis=str(time.time())+ '::' + tweet
            saveFile = open('locandtext.csv','a')
            saveFile.write(saveThis)
            saveFile.write('\n')
            saveFile.close()
            return True
        except BaseException, e:
            print 'Failed on data' , str(e)
            time.sleep(5)

    def on_error(self, status):
        print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream (auth , Listener())
twitterStream.filter(track=["Earthquake"] )

data
的值不包含标记
,“text:”
,因此
拆分的结果是一个项目列表,其中包括整个文本

为避免索引器
验证,“文本”:“出现在数据中:

token = ',"text":"'
if token in data:
    text = data.split(token)[1]

data
的值不包含标记
,“text:”
,因此
拆分的结果是一个项目列表,其中包括整个文本

为避免索引器
验证,“文本”:“出现在数据中:

token = ',"text":"'
if token in data:
    text = data.split(token)[1]

你想用这段代码实现什么?我想获取位置、时间和tweets并保存在csv文件中。我正在使用data.split获取推文,但无法获取位置。您可以发布您的示例数据吗?1457410676.51::RT@robbwolf:雷诺地震!古怪的。1457410777.98::13:19地震https:\/\/t.co\/2X8FaipZsW 1457410814.04::1.7级地震。距离美国加利福尼亚州安扎12英里https:\/\/t.co\/1GWexXmLom 1457410819.04::1.7级地震。来自美国加利福尼亚州安扎市的12英里https:\/\/t.co\/fl5mdx7bhs谢谢你,我现在获得了正确的输出,你想用这段代码实现什么?我想获得位置、时间和推文,并保存在csv文件中。我正在使用data.split获取推文,但无法获取位置。您可以发布您的示例数据吗?1457410676.51::RT@robbwolf:雷诺地震!古怪的。1457410777.98::13:19地震https:\/\/t.co\/2X8FaipZsW 1457410814.04::1.7级地震。距离美国加利福尼亚州安扎12英里https:\/\/t.co\/1GWexXmLom 1457410819.04::1.7级地震。来自美国加利福尼亚州安扎的12英里https:\/\/t.co\/fl5mdx7bhs谢谢,我现在获得了正确的输出