使用Python,Tweepy,插入光标和Arcpy

时间:2015-06-17 22:00:10

标签: python insert cursor tweepy arcpy

我是Python的新手,请原谅我缺乏知识哈哈但是由于某种原因我不能让Python在我的数据库中插入行。这就是我所拥有的:

import sys, arcpy, datetime, tweepy


consumer_key = " "
consumer_secret = " "
access_token = " "
access_token_secret = " "

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

table = r"C:\....dbf"

rows = arcpy.InsertCursor(table)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:


            user = status.user.screen_name
            tweet = status.text
            coord_x = status.coordinates['coordinates'][0]
            coord_y = status.coordinates['coordinates'][1]
            date_utc = status.created_at
            h_m_s_utc = (str(status.created_at.hour))+':'+(str(status.created_at.minute))+':'+(str(status.created_at.second))
            date_est = datetime.datetime.now()
            h_m_s_est = (str(date_est.hour))+':'+(str(date_est.minute))+':'+(str(date_est.second))


            row.user_name=user
            row.tweet=tweet
            row.coord_x=coord_x
            row.coord_y=coord_y
            row.date_utc=date_utc
            row.h_m_s_utc=h_m_s_utc
            row.date_est=date_est
            rows.insertRow(row)
            del row, rows
            insert_table= r"C:\....dbf"
            insert_row(insert_table)

        print user
        print tweet

    except:
        # If there are no coordinates for a tweet, then pass
        pass

def on_error(self, status_code):
    print >> sys.stderr, 'Encountered error with status code:', status_code
    return True # Don't kill the stream

def on_timeout(self):
    print >> sys.stderr, 'Timeout...'
    return True # Don't kill the stream

# ----------------Script execution----------------
listener = tweepy.streaming.Stream(auth, CustomStreamListener())
listener.filter(track=[' love ', '#love'])

我很确定它与row.rowID有关。

对不起,如果这是一场灾难!非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我看起来你忘了为插入游标调用数据访问(.da)方法。

with arcpy.da.InsertCursor(in_table, field_names) as inCursor:
    for row in rows:
        inCursor.insertRow(row) # example

-OR -

inCursor = arcpy.da.InsertCursor(in_table, field_names)
for row in rows:
    cursor.insertRow(row) # example
del inCursor # make sure to delete cursor if you do it this way as to avoid data lock.

此外,如果您只想要插入光标方法,则可以

from arcpy import da

有关详细信息,请查看: http://resources.arcgis.com/en/help/main/10.2/index.html#//018w0000000t000000