Reputation: 4122
If my understanding of the below code I have is correct Python is returning a list from a the source code of a web page that I am scraping (the data is not stored within any HTML tags, hence why I'm not using XPath or anything to scrape the data I want):
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):
player['Name'],',',player['FirstName'],',',player['LastName']
I've worked out how put a comma between the data elements I am printing, but this is printing with a space between each element and the comma, in the following fashion:
Name , FirstName , LastName
How can I get it so that it is printed like this instead:
Name,FirstName,LastName
Thanks
Upvotes: 0
Views: 246
Reputation: 180441
Use str.format
print("{Name},{FirstName},{LastName}".decode().format(**player)) # gets values of keys specified in format args from player
Upvotes: 1
Reputation:
Simply player['Name'] + ',' + player['FirstName'] + ',' + player['LastName']
will do what you want. The reason for this is that ,
concatenates <space><string>
where <space>
is whitespace and <string>
is your string.
Upvotes: 3