我嘗試使用下面的代碼:有麻煩把字符串到一個列表(錯誤消息)
for x in story:
var1 = str(x)
var1 = var1.replace("<p>", "\n")
var1 = var1.replace("</p>", "")
story[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.
我得到的錯誤信息是:
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
@tiz答案是正確的,你可能需要編輯和發佈你的完整代碼,因爲你可能會覆蓋一些重要的東西,我敢打賭'str' – BrainStorm