Python方法中的错误。列表
我是Python新手。我正在写一个简单的类,但我有一个错误 我的班级:Python方法中的错误。列表,python,list,Python,List,我是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, tim
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
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\anlopes\workspace\redes\u soc\src\twitterC.py”,第42行,在
x、 UpdateWitterStatus([{“url”:”http://xxxx.com/?cat=31,“msg”:“查看一些字符串…,”}])
文件“C:\Documents and Settings\anlopes\workspace\redes\u soc\src\twitterC.py”,第28行,在UpdateWriterStatus中
short=self.shortUrl(更新[“url”])#您的网址
TypeError:列表索引必须是整数,而不是str
关于如何解决这个问题有什么线索吗
致以最诚挚的问候,您对UpdateWitterStatus的呼叫似乎只需要去掉方括号:
x.updateTwitterStatus({"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "})
您正在传递一个包含单个dictionary元素的列表。看起来该方法只需要一个带有“url”和“msg”键的字典
在Python中,
{…}
创建一个字典,[…]
创建一个列表。错误消息告诉您需要知道的一切。它表示“列表索引必须是整数,而不是str”,并指向代码short=self.shortUrl(更新[“url]”)
。因此,python解释器显然认为update
是一个列表,而“url”
不是列表中的有效索引
由于update
是作为参数传入的,因此我们必须查看它的来源。它看起来像[{…}]
,这意味着它是一个列表,里面只有一个字典。可能您只想传递字典,因此在调用x.updateWitterStatus时请删除方括号
调试的第一条规则是假设错误消息是正确的,并且您应该按照字面意思理解它 谢谢你的回复。帮了大忙。谢谢你的回复。致以最诚挚的问候,请记住,您也可以投票选出好的答案(就像我刚才为史蒂夫所做的那样),如果它被选中,我认为它是好的