如何使用Python拆分字符串并删除CSV中不完整的行?

如何使用Python拆分字符串并删除CSV中不完整的行?,python,html,csv,scrapy,Python,Html,Csv,Scrapy,目前,我正在使用Python中的Scrapy从网站中提取html表以写入csv。我已经设法得到了我想要的数据,但是最终的格式不是我所需要的100%。链接是,这是我的代码: import scrapy class XGSpider(scrapy.Spider): name = 'expectedGoals' start_urls = [ 'https://fbref.com/en/comps/9/schedule/Premier-League-Scores-a

目前,我正在使用Python中的Scrapy从网站中提取html表以写入csv。我已经设法得到了我想要的数据,但是最终的格式不是我所需要的100%。链接是,这是我的代码:

import scrapy

class XGSpider(scrapy.Spider):

    name = 'expectedGoals'

    start_urls = [
        'https://fbref.com/en/comps/9/schedule/Premier-League-Scores-and-Fixtures',
    ]

    def parse(self, response):

        for row in response.xpath('//*[@id="sched_ks_3232_1"]//tbody/tr'):
            yield {
                'home': row.xpath('td[4]//text()').extract_first(),
                'homeXg': row.xpath('td[5]//text()').extract_first(),
                'score': row.xpath('td[6]//text()').extract_first(),
                'awayXg': row.xpath('td[7]//text()').extract_first(),
                'away': row.xpath('td[8]//text()').extract_first()
            }
因此,要将文件保存到csv中,请在终端中键入:

scrapy crawl expectedGoals --output exG.csv
我得到这个csv:

home,homeXg,score,awayXg,away
Liverpool,1.9,4–1,1.0,Norwich City
West Ham,0.7,0–5,3.3,Manchester City
Burnley,0.7,3–0,0.8,Southampton
Watford,0.9,0–3,0.7,Brighton
Bournemouth,1.0,1–1,1.0,Sheffield Utd
Crystal Palace,0.8,0–0,0.9,Everton
Tottenham,2.6,3–1,0.6,Aston Villa
Newcastle Utd,0.5,0–1,0.9,Arsenal
Leicester City,0.6,0–0,0.6,Wolves
Manchester Utd,2.1,4–0,0.8,Chelsea
,,,,
Arsenal,1.0,2–1,1.3,Burnley

.
.
.
.

我想使用
-
作为分隔符,将
分数
分为
homeScore
awayScore
字段。此外,我还想弄清楚,如果字段为空,如何完全删除一行,如上所述。我不知道怎么做?

首先,加载csv并将其转换为熊猫

df = pd.read_csv("exG.csv") 
为了替代csv,我正在创建人工数据\u csv并将其转换为熊猫

df = pd.read_csv("exG.csv") 
data_csv = [{'home': 'Liverpool', 'score': '4-1', 'away': 'Norwich City'},
            {'home': 'West Ham,', 'score': '9-5', 'away': "Manchester City"},
            {'home': 'Burnley', 'score': '3-0', 'away': 'Southampton'}]



df = pd.DataFrame(data_csv)
我想使用
-
作为分隔符将分数拆分为homeScore和awayScore字段


哪个字段是空的?我找不到?有可能提供csv吗?如果我的或其他人有帮助,不要忘记接受它。谢谢