Python的.strip()不保存在if语句之外?

Python的.strip()不保存在if语句之外?,python,scrapy,stocks,Python,Scrapy,Stocks,我从网页上抓取数据。我遇到的一个问题是,它占用了大量空白,我选择使用别人建议的.strip。不过我遇到了一个问题 if a.strip(): print a if b.strip(): print b 返回: a1 b1 . . . 但这是: if a.strip(): aList.append(a) if b.strip(): bList.append(b) print aList, bList 返回以下内容: a1 b1 我试图模拟我删除

我从网页上抓取数据。我遇到的一个问题是,它占用了大量空白,我选择使用别人建议的.strip。不过我遇到了一个问题

if a.strip():
    print a
if b.strip():
    print b
返回:

a1
b1
.
.
.
但这是:

if a.strip():
    aList.append(a)
if b.strip():
    bList.append(b)
print aList, bList
返回以下内容:

a1



    b1
我试图模拟我删除的空白。这里去掉,但你明白了。不管出于什么原因,它在列表中添加了空格,即使我告诉它不要这样做。我甚至可以在if语句中打印列表,它也可以正确显示,但无论出于什么原因,当我决定在if语句之外打印时,它都无法按我的预期工作

这是我的全部代码:

# coding: utf-8
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.exporter import CsvItemExporter
import re
import csv
import urlparse
from stockscrape.items import EPSItem
from itertools import izip

class epsScrape(BaseSpider):
        name = "eps"
        allowed_domains = ["investors.com"]
        ifile = open('test.txt', "r")
        reader = csv.reader(ifile)
        start_urls = []
        for row in ifile:
                url = row.replace("\n","")
                if url == "symbol":
                        continue
                else:
                        start_urls.append("http://research.investors.com/quotes/nyse-" + url + ".htm")
        ifile.close()

        def parse(self, response):
                f = open("eps.txt", "a+")
                sel = HtmlXPathSelector(response)
                sites = sel.select("//div")
#               items = []
                for site in sites:
                        symbolList = []
                        epsList = []
                        item = EPSItem()
                        item['symbol'] = site.select("h2/span[contains(@id, 'qteSymb')]/text()").extract()
                        item['eps']  = site.select("table/tbody/tr/td[contains(@class, 'rating')]/span/text()").extract()
                        strSymb = str(item['symbol'])
                        newSymb = strSymb.replace("[]","").replace("[u'","").replace("']","")
                        strEps = str(item['eps'])
                        newEps = strEps.replace("[]","").replace(" ","").replace("[u'\\r\\n","").replace("']","")
                        if newSymb.strip():
                                symbolList.append(newSymb)
#                               print symbolList
                        if newEps.strip():
                                epsList.append(newEps)
#                               print epsList
                        print symbolList, epsList
                for symb, eps in izip(symbolList, epsList):
                        f.write("%s\t%s\n", (symb, eps))
                f.close()
strip不会在位修改字符串。它返回一个新字符串,其中去掉了空格

>>> a = '    foo      '
>>> b = a.strip()
>>> a
'    foo      '
>>> b
'foo'

我知道是什么引起了混乱。它是我声明变量/列表的位置。我是在for循环中声明它的,所以每次它迭代时都会重写它,对于if语句,一个空白列表或变量都是false的结果。

您应该执行bList.appendb.strip文档中说的是什么?字符串是不可变的。strip无法更改该值,因此它返回一个新的strip string对象。@Matjin看到这就是我读到的内容,所以我想尝试将它赋给另一个变量,但这并没有改变任何事情。我相信我尝试过类似的方法:if a.strip:b=a if c.strip:d=c,但当我尝试在if语句之外打印时,它会打印所有的空格。作为旁注,Python有几个内置类型数字、布尔值、字符串、元组,frozensets是不可变的。@正如我所说,a.strip返回一个新字符串-a未修改。因此,当您写入a.strip:b=a时,它会将b设置为原始的非压缩变量a。