【问题标题】:Python's .strip() doesnt save outside of if statement?Python .strip() 不保存在 if 语句之外?
【发布时间】:2013-11-19 18:26:06
【问题描述】:

我从网页中抓取数据。我遇到的一个问题是它会拉出很多空格,并且我选择使用 .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 语句中打印列表,它也可以正确显示,但无论出于何种原因,当我决定在 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()

【问题讨论】:

  • 你应该这样做bList.append(b.strip())
  • 文档strip做了什么?
  • 字符串是不可变的; .strip() 不能改变值,所以它返回一个 new 剥离的字符串对象。
  • @Matjin 看到这是我读到的,所以我想尝试将它分配给另一个变量,但这并没有改变任何东西。

标签: python scrapy stocks


【解决方案1】:

strip 不会就地修改字符串。它返回一个去掉空格的新字符串。

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

【讨论】:

  • 我相信我尝试了类似于我这样做的地方: if a.strip(): b = a if c.strip(): d = c 但是当我尝试在外部打印时它打印的所有空格的 if 语句。
  • 作为旁注,Python 有几种内置类型(数字、布尔值、字符串、元组、冻结集)是不可变的。
  • @Resin 正如我所说,a.strip() 返回一个新字符串 - a 没有被修改。所以当你写if a.strip(): b = a时,它会将b设置为原始(未剥离的)变量a
【解决方案2】:

我弄清楚是什么造成了混乱。它是我声明变量/列表的位置。我在 for 循环中声明它,所以每次迭代它都会重写,并且一个空白列表或变量与我的 if 语句的 false 结果相同。

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2012-10-31
    • 2015-07-02
    相关资源
    最近更新 更多