我是新來的Python。我正在寫一個簡單的課程,但我遇到了一個錯誤。Python方法中的錯誤。列出
我的類:
import config # Ficheiro de configuracao
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python
class TwitterC:
def logToDatabase(self, tweet, timestamp):
# Will log to the database
database = sqlite3.connect('database.db') # Create a database file
cursor = database.cursor() # Create a cursor
cursor.execute("CREATE TABLE IF NOT EXISTS twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT);") # Make a table
# Assign the values for the insert into
msg_ins = tweet
timestamp_ins = timestamp
values = [msg_ins, timestamp_ins]
# Insert data into the table
cursor.execute("INSERT INTO twitter(tweet, timestamp) VALUES(?, ?)", values)
database.commit() # Save our changes
database.close() # Close the connection to the database
def shortUrl(self, url):
bit = bitly_api.Connection(config.bitly_username, config.bitly_key) # Instanciar a API
return bit.shorten(url) # Encurtar o URL
def updateTwitterStatus(self, update):
short = self.shortUrl(update["url"]) # Vou encurtar o URL
update = update["msg"] + short['url']
# Will post to twitter and print the posted text
twitter_api = twitter.Api(consumer_key=config.twitter_consumer_key,
consumer_secret=config.twitter_consumer_secret,
access_token_key=config.twitter_access_token_key,
access_token_secret=config.twitter_consumer_secret)
status = twitter_api.PostUpdate(update) # Fazer o update
msg = status.text # Vou gravar o texto enviado para a variavel 'msg'
# Vou gravar p a Base de Dados
self.logToDatabase(msg, time.time())
print msg
x = TwitterC()
x.updateTwitterStatus([{"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "}])
的錯誤是:
Traceback (most recent call last):
File "C:\Documents and Settings\anlopes\workspace\redes_soc\src\twitterC.py", line 42, in <module>
x.updateTwitterStatus([{"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "}])
File "C:\Documents and Settings\anlopes\workspace\redes_soc\src\twitterC.py", line 28, in updateTwitterStatus
short = self.shortUrl(update["url"]) # Vou encurtar o URL
TypeError: list indices must be integers, not str
如何解決它任何線索?
最好的問候,
記住你也可以給予好評很好的答案(像我只是做了爲史蒂夫),如果它的勾選,我相信這是很好的。 :) – 2011-03-31 21:01:32