python .replace留下空行

时间:2013-11-15 22:14:52

标签: python string scrapy

我正在使用scrapy从网站上提取数据。纯粹的版本是这样的:

{eps: 25}
{eps:[]}
{eps:[]}
{eps:[]}
{eps: 50}
{eps:[]}
{eps:[]}
{eps:[]}

现在我不确定为什么会出现空白,但我可以用.replace删除它们。问题是,当我使用.replace时,结果是这样的:

25



50



# Code comment to show extra spaces.

我已尝试.split.sub.strip无济于事。我不知道还有什么可以尝试的。

更新:

添加源代码

# 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

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("//tbody/tr")
                items = []
                for site in sites:
                        item = EPSItem()
                        item['eps']  = site.select("td[contains(@class, 'rating')]/span/text()").extract()
                        strItem = str(item)
                        newItem = strItem.replace(" ","").replace("'","").replace("{eps:[","").replace("]}","").replace("u","").replace("\\r\\n",'').replace('$
                        f.write("%s\n" % newItem)
                f.close()

text.txt中有一个股票代码:

MSFT
A
H

等等等等

1 个答案:

答案 0 :(得分:6)

空行包含换行符;也替换\n

如果您发现最终删除所有换行符,则拆分换行符并删除所有空字符串值:

outputstring = '\n'.join([line for line in inputstring.splitlines() if line.strip()])

这将删除所有空行,并使用新换行符重新连接剩余的非空行。

如果您通过打印或写入文件逐行生成输出,那么当行为空时,只需不打印或写

newItem = newItem.replace(.., ..)
if newItem.strip():
    print newItem
    f.write('{}\n'.format(newItem))

if语句测试的行不仅包含空格。