【发布时间】:2020-04-19 08:34:38
【问题描述】:
我可以下载文件:
seasons = [2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
epl_tables = {}
epl_seasons = {}
for year in seasons:
start_year = str(year)[-2:]
end_year = str(year+1)[-2:]
season = start_year + end_year
epl_seasons[season] = pd.read_csv("https://www.football-data.co.uk/mmz4281/{}{}/E0.csv".format(start_year, end_year)).dropna(how='all')
epl_tables[season] = league(epl_seasons[season])
这很好用。
但是,当我尝试通过将 2004 添加到季节来添加 2004-05 季节时,出现问题并且代码失败。
seasons = [2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
此问题是由于 csv 文件第 337 到 345 行中裁判姓名前的空格引起的。
我可以通过手动删除空白然后从磁盘加载来解决这个问题,但显然它并不理想。
我尝试了各种方法让它工作,如下所示,但似乎没有任何工作,
epl_seasons[season] = pd.read_csv("https://www.football-data.co.uk/mmz4281/{}{}/E0.csv".format(start_year, end_year), delimiter=',', encoding="utf-8", skipinitialspace=True).dropna(how='all')
一个潜在的复杂情况是,当我在 excel 中打开文件时,该空间显示为空白,但随后我在 libreCalc 中打开它(在 Ubuntu 中,这是我正在使用的),它显示为未知字符一个问题标记在一个倾斜 45 度的黑盒子里。在下面的链接中查看 PeterMau 的答案,看看这个未知角色的样子。
https://ask.libreoffice.org/en/question/113125/characters-turned-into-question-marks/
谁能告诉我谁能自动删除这些空格/未知字符?
【问题讨论】:
-
由于您使用的是 Pandas,您是否可以仅隔离有问题的行(我猜是裁判姓名列)并在尝试之前删除 CSV 文件中字符之前的所有空格首先将所有内容作为字典导入?
-
这可以作为手动工作,但我想自动化这个过程。
-
我在下面提供了我的解决方案。我认为这不再是手动过程。使用 pandas 读取 CSV 文件后,您只需要一行。
标签: python-3.x pandas ubuntu