Python 无法将字符串放入列表(错误消息)

Python 无法将字符串放入列表(错误消息),python,html,parsing,Python,Html,Parsing,我正在尝试使用以下代码: for x in story: var1 = str(x) var1 = var1.replace("<p>", "\n") var1 = var1.replace("</p>", "") story[x] = var1 故事中x的: var1=str(x) var1=var1。替换(“”,“\n”) var1=var1。替换(“”,“”) 故事[x]=var1 删除段落标记,并插入换行符,然后将其重新插入变量

我正在尝试使用以下代码:

for x in story:
    var1 = str(x)
    var1 = var1.replace("<p>", "\n")
    var1 = var1.replace("</p>", "")
    story[x] = var1
故事中x的
:
var1=str(x)
var1=var1。替换(“”,“\n”)
var1=var1。替换(“

”,“”) 故事[x]=var1
删除段落标记,并插入换行符,然后将其重新插入变量。字符串如下所示:

Panera Bread (NASDAQ: <a class="ticker" href="/stock/pnra#NASDAQ">PNRA</a>) is down 6 percent today over expectations of food inflation of 4.5% in Q3 and 5% for Q4. In addition, Panera Will Raise Menu Prices in Q4.

PNRA recently posted second quarter 2011 earnings of $1.18 per share. Reported earnings also outpaced the prior-year quarter earnings of 85 cents per share. 

But shares were also lower ahead of the opening bell after the company reported weaker-than-expected same-store sales figures for its recent quarter late Tuesday. Its profit of $1.18 a share topped analysts' consensus call by a penny.

For the twenty-six weeks ended June 28, 2011, net income was $68 million, or $2.27 per diluted share. These results compare to net income of $53 million, or $1.67 per diluted share, for the twenty-six weeks ended June 29, 2010, and represent a 36% year-over-year increase in diluted earnings per share.
Panera Bread(NASDAQ:)今天下跌6%,超出了第三季度食品通胀4.5%和第四季度5%的预期。此外,Panera将在第四季度提高菜单价格。 PNRA最近公布了2011年第二季度每股收益1.18美元。报告的收益也超过了上一年第四季度每股85美分的收益。 但在周二晚些时候该公司公布的最近一季度同店销售额低于预期后,开盘前股价也下跌。其每股1.18美元的利润超过了分析师的普遍预期。 截至2011年6月28日的26周内,净收入为6800万美元,或摊薄后每股2.27美元。这些结果与截至2010年6月29日的26周净收入5300万美元或稀释每股1.67美元相比,表明稀释每股收益同比增长36%。 我收到的错误消息是:

Traceback (most recent call last):
  File "C:\Python27\Sample Programs\Get Stuff from Pages\Pages and Stuff 0.1.py", line 34, in <module>
    story[x] = var1
TypeError: list indices must be integers, not Tag
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Sample Programs\Get Stuff from Pages\Pages and Stuff 0.1.py”,第34行,在
故事[x]=var1
TypeError:列表索引必须是整数,而不是标记

您可能希望将项目附加到列表中:

story.append(var1)

for
循环将list
story
的元素迭代替换为
x
变量,而
[]
list指令需要元素索引。这导致了错误

l = ['a','b']
print l[0]
print l['a'] // type error
编辑:我错过了故事不是由字符串组成的。这些更改可以完成以下工作:

story = [str(x).replace("<p>","\n").replace("<\p>","") for x in story]
story=[str(x).replace(“”,“\n”).replace(“,”)for x in story]

注意:现在
故事
由字符串而不是标记组成。

回溯(最近一次调用):文件“C:\Python27\Sample Programs\Get Stuff from Pages\Pages and Stuff 0.1.py”,故事中第30行=[x.replace(“”,“\n”)。replace(

,”)代表故事中的x]TypeError:“NoneType”对象是不可调用的这是我在执行该操作时得到的结果,它工作正常,但现在我无法连接或连接列表中的所有unicode字符串-这会给我带来错误。知道怎么做吗?@Andrew,我想不出有什么原因。。。我所能建议的就是创建新问题并发布更详细的代码。这进展非常缓慢,冻结了我的系统。@tiz回答是对的,您可能需要编辑并发布完整的代码,因为您可能会覆盖一些重要的内容,我打赌它
str