print语句中的代码中的换行符,导致值打印到屏幕中的空格

时间:2014-08-23 14:00:16

标签: python scrapy

我有一些代码是从列表中打印项目,每个项目之间没有空格,并且用逗号分隔它们:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from scrapy.item import Item
from scrapy.spider import BaseSpider
import re
import json

class ExampleSpider(CrawlSpider):
    name = "goal4"
    allowed_domains = ["whoscored.com"]
    start_urls = ["http://www.whoscored.com"]
    download_delay = 1

    rules = [Rule(SgmlLinkExtractor(allow=(''),deny=('/News', '/Fixtures', '/Graphics', '/Articles', '/Live', '/Matches', '/Explanations', '/Glossary', '/Players', 'ContactUs', 'TermsOfUse'),), follow=False, callback='parse_item')]

    def parse_item(self, response):

        sel = Selector(response)

        match1 = re.search(re.escape("DataStore.prime('stage-player-stat', defaultTeamPlayerStatsConfigParams.defaultParams , ") \
                     + '(\[.*\])' + re.escape(");"), response.body)

        if match1 is not None:
            playerdata1 = match1.group(1)

            for player in json.loads(playerdata1):

                print ("{TeamId},{PlayerId},{Name},{FirstName},{LastName},{TeamName},{PositionText},{PositionLong} \
            ,{Age} \
            ,{Height},{Weight},{GameStarted},{SubOn},{SubOff} \
            ,{Goals},{OwnGoals},{Assists},{Yellow},{SecondYellow},{Red} \
            ,{TotalShots} \
            ,{ShotsOnTarget},{ShotsBlocked},{TotalPasses},{AccuratePasses},{KeyPasses} \
            ,{TotalLongBalls},{AccurateLongBalls},{TotalThroughBalls},{AccurateThroughBalls} \
            ,{AerialWon},{AerialLost},{TotalTackles},{Interceptions},{Fouls} \
            ,{Offsides},{OffsidesWon},{TotalClearances},{WasDribbled},{Dribbles} \
            ,{WasFouled} \
            ,{Dispossesed},{Turnovers},{TotalCrosses},{AccurateCrosses}".decode().format(**player))

这适用于列表中的元素,直到第一个换行符。这意味着代表代码中的换行符,而不是屏幕上的任何中断。直接在中断后的所有元素都打印出它与前一个元素之间的巨大空间。那些介于打破之间的打印很好,直到下一次休息等。

我想知道的是,如何在不影响打印效果的情况下将这些代码分解为多行?

由于

3 个答案:

答案 0 :(得分:2)

以pythonic方式创建一个非常长的字符串,而不是:

s = 'this string is soooo\
     ooo loooong'

由于第二行中的前导空格,打印为:

this string is soooo       ooo loooong

您可以使用:

s = ('this string is soooo'
     'ooo loooong')

打印为:

this string is sooooooo long

在你的情况下你应该:

print ('{TeamId},{PlayerId},{Name},{FirstName},{LastName},{TeamName},{PositionText},{PositionLong}'
       ',{Age}'
       ...
       ',{Dispossesed},{Turnovers},{TotalCrosses},{AccurateCrosses}').decode().format(**player)

答案 1 :(得分:1)

使用"""三重引号和replace

print ("""{TeamId},{PlayerId},{Name},{FirstName},{LastName},{TeamName},{PositionText},{PositionLong} 
                ,{Age} 
                ,{Height},{Weight},{GameStarted},{SubOn},{SubOff} 
                ,{Goals},{OwnGoals},{Assists},{Yellow},{SecondYellow},{Red} 
                ,{TotalShots} 
                ,{ShotsOnTarget},{ShotsBlocked},{TotalPasses},{AccuratePasses},{KeyPasses} 
                ,{TotalLongBalls},{AccurateLongBalls},{TotalThroughBalls},{AccurateThroughBalls} 
                ,{AerialWon},{AerialLost},{TotalTackles},{Interceptions},{Fouls} 
                ,{Offsides},{OffsidesWon},{TotalClearances},{WasDribbled},{Dribbles} 
                ,{WasFouled} 
                ,{Dispossesed},{Turnovers},{TotalCrosses},{AccurateCrosses}""".replace(" ","").decode().format(**player))

或者在您自己的代码中:

print ("{TeamId},{PlayerId},{Name},{FirstName},{LastName},{TeamName},{PositionText},{PositionLong} \
            ,{Age} \
            ,{Height},{Weight},{GameStarted},{SubOn},{SubOff} \
            ,{Goals},{OwnGoals},{Assists},{Yellow},{SecondYellow},{Red} \
            ,{TotalShots} \
            ,{ShotsOnTarget},{ShotsBlocked},{TotalPasses},{AccuratePasses},{KeyPasses} \
            ,{TotalLongBalls},{AccurateLongBalls},{TotalThroughBalls},{AccurateThroughBalls} \
            ,{AerialWon},{AerialLost},{TotalTackles},{Interceptions},{Fouls} \
            ,{Offsides},{OffsidesWon},{TotalClearances},{WasDribbled},{Dribbles} \
            ,{WasFouled} \
            ,{Dispossesed},{Turnovers},{TotalCrosses},{AccurateCrosses}".replace(" ","").decode().format(**player))

答案 2 :(得分:0)

您也可以简单地将字符串分解成碎片,然后将它们与加号连接起来。例如'你好'+'世界'