我想在CSV中的每两行之后创建空白行。有办法吗
这是我的密码:
# -*- coding: utf-8 -*-
import scrapy
import re
class AaSpider(scrapy.Spider):
name = 'aa'
allowed_domains = ['covers.com']
year = int(input('Enter the Year:'))
s = int(input('Enter Start of the month:'))
e = int(input('Enter End of the month:'))
# start_urls = ['https://www.covers.com/sports/MLB/matchups?selectedDate=2021-4-5']
def start_requests(self):
for i in range((self.s), (int(self.e)+1)):
url = f'https://www.covers.com/sports/MLB/matchups?selectedDate={self.year}-{i}-1'
for a in range(1,31):
ab = ((url[:-1]) + str(a))
yield scrapy.Request(url = ab,callback= self.parse)
def filter(self , x):
regex = re.compile('[^a-zA-Z]')
regex.sub('', x)
def parse(self, response):
# count = 1
for count ,data in enumerate(response.xpath('//div[@class="cmg_matchup_line_score"]/table/tbody/tr')):
if count % 3 == 0:
a = data.xpath('./td[position() = count((//div[@class="cmg_matchup_line_score"])[1]/table/thead//th[text() = "X"]/preceding-sibling::th)+1]/text()').extract()
yield{
'Date': '',
'Team': '',
'1': '',
'2': '',
'3': '',
'4': '',
'5': '',
'6': '',
'7': '',
'8': '',
'9': '',
'X': '',
'R': '',
'ML': '',
'O/U': '',
'H': '',
'E': '',
}
else:
a = data.xpath('./td[position() = count((//div[@class="cmg_matchup_line_score"])[1]/table/thead//th[text() = "X"]/preceding-sibling::th)+1]/text()').extract()
yield{
'Date': ((response.url).split('=')[1]),
'Team': data.xpath('./td[1]/text()').get(),
'1': data.xpath('./td[2]/text()').get(),
'2': data.xpath('./td[3]/text()').get(),
'3': data.xpath('./td[4]/text()').get(),
'4': data.xpath('./td[5]/text()').get(),
'5': data.xpath('./td[6]/text()').get(),
'6': data.xpath('./td[7]/text()').get(),
'7': data.xpath('./td[8]/text()').get(),
'8': data.xpath('./td[9]/text()').get(),
'9': data.xpath('./td[10]/text()').get(),
'X': [i for i in a if not i.replace(" ", "").replace("_", "").isalpha()],
'R': data.xpath('./td[11]/text()').get(),
'ML': data.xpath('./td[12]/text()').get(),
'O/U': data.xpath('./td[13]/text()').get(),
'H': data.xpath('./td[14]/text()').get(),
'E': data.xpath('./td[15]/text()').get(),
}
有没有办法在每两行之后创建空白行。多谢各位
我试过这种方法,但不起作用。我得到的结果就像有时是3行之后的空白行,有时是一行之后的空白行
我不熟悉刮痧。但是,我会尝试利用:
您可以在“for”循环外部初始化count,并在“for”循环内部将其递增1。只要上述条件为真,您就可以在csv中插入数据之前写入插入空行的逻辑。当计数为3、6、9、12等时,这实际上将插入一个空行,这意味着2行将是数据,第3行将是空行
相关问题 更多 >
编程相关推荐