Python TypeError:字符串索引必须是整数datetime

Python TypeError:字符串索引必须是整数datetime,python,string,integer,typeerror,Python,String,Integer,Typeerror,我正在尝试转换日期时间,但我不知道如何将字符串转换为整数。我尝试在(条目[“created_at”]之后添加[0],但没有添加 代码: temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC) TypeError: string indices must be integers $对于数据中的项: `$` convertedTweet = {}

我正在尝试转换日期时间,但我不知道如何将字符串转换为整数。我尝试在(条目[“created_at”]之后添加[0],但没有添加

代码:

temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
TypeError: string indices must be integers
$
对于数据中的项:

   `$` convertedTweet = {}

for entry in item:
   convertedTweet["created_at"] = item["created_at"]
   fmt = '%Y-%m-%dT%H:%M:%SZ'
   temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
   print temp.strftime(fmt)
错误:

temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
TypeError: string indices must be integers

看起来
item
是一个字典(当您这样做时-
item[“created_at”]
,它不会为您出错)

如果是这样的话,当你在字典中循环-

for entry in item:
entry
表示字典中的键,它们很可能是字符串。因此,当您尝试执行-
entry['created_at']
时,它会出错,因为您尝试对字符串使用字符串索引,这是不可能的

看起来您根本不需要循环,您可以直接访问-
项[“created_at”]
-以获取日期时间。例如-

convertedTweet["created_at"] = item["created_at"] 
temp = datetime.strptime(item["created_at"],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
print temp.strftime('%Y-%m-%dT%H:%M:%SZ')

我不知道如何将我的字符串设置为整数
int(string)
呢?项目已转换为tweet,更新代码很高兴它对您有用。如果答案有用,我还想请求您接受答案(通过单击答案左侧的勾号),这将对社区有帮助。我想我做到了。这是我的第一篇帖子。再次感谢你的帮助