2014-12-07 34 views
1

我得到了Jamie Bull和PM 2Ring的建议,使用CSV模块输出我的网页报废。我差不多完成了,但是有一些用冒号或连字符分隔的已分析项。我希望将这些项目分解为当前列表中的两个项目。Python:将解析的项目拆分为CSV文件

电流输出:

GB,16,19,255,1,26:40,19,13,4,2,6-120-1,255,57 ,4.5,80,21,3.8,175,23-33,4.9,3,14,1,4,38.3,8,65,1,0 Sea,36,25,398,1,33:20 ,25,8,13,4,4-11,1-1,398,66,6.0,207,37,5.6,191,19-28,6.6,1,0,0,2,33.0,4,69,2,1

所需的输出:(这些问题/差异以粗体显示)

GB,16,19,255,1 ,26,40,19,13,4,2,6,120,1,255,57,4.5,80,21,3.8,175,23,33,4.9,3 ,14,1,4,38,3,8,65,1,0 Sea,36,25,398,1,33,20,25,8,13,4,4,11,1,1,398,66,6,207,37,5.6,191,19,28,6.6,1,0,0,2,33,4,69,2,1

我不确定在哪里或如何进行这些更改。我也不知道是否需要正则表达式。很明显,我可以在记事本或Excel中处理,但我的目标是在Python中处理所有这些。

如果你运行的程序,上述结果是从2014年赛季,一周1

import requests 
import re 
from bs4 import BeautifulSoup 
import csv 

year_entry = raw_input("Enter year: ") 

week_entry = raw_input("Enter week number: ") 

week_link = requests.get("http://sports.yahoo.com/nfl/scoreboard/?week=" + week_entry + "&phase=2&season=" + year_entry) 

page_content = BeautifulSoup(week_link.content) 

a_links = page_content.find_all('tr', {'class': 'game link'}) 

csvfile = open('NFL_2014.csv', 'a') 

writer = csv.writer(csvfile) 

for link in a_links: 
     r = 'http://www.sports.yahoo.com' + str(link.attrs['data-url']) 
     r_get = requests.get(r) 
     soup = BeautifulSoup(r_get.content) 
     stats = soup.find_all("td", {'class':'stat-value'}) 
     teams = soup.find_all("th", {'class':'stat-value'}) 
     scores = soup.find_all('dd', {"class": 'score'}) 

     try: 
       away_game_stats = [] 
       home_game_stats = [] 
       statistic = [] 
       game_score = scores[-1] 
       game_score = game_score.text 
       x = game_score.split(" ") 
       away_score = x[1] 
       home_score = x[4] 
       home_team = teams[1] 
       away_team = teams[0] 
       away_team_stats = stats[0::2] 
       home_team_stats = stats[1::2] 
       away_game_stats.append(away_team.text) 
       away_game_stats.append(away_score) 
       home_game_stats.append(home_team.text) 
       home_game_stats.append(home_score) 
       for stats in away_team_stats: 
         text = stats.text.strip("").encode('utf-8') 
         away_game_stats.append(text) 


       writer.writerow(away_game_stats) 

       for stats in home_team_stats: 
         text = stats.text.strip("").encode('utf-8') 
         home_game_stats.append(text) 

       writer.writerow(home_game_stats) 

     except: 
       pass 


csvfile.close()       

任何帮助是极大的赞赏。这是我的第一个程序,搜索这个板子是一个很好的资源。

感谢,

JT

+0

附注:除/ pass之外是危险的,因为它隐藏了任何类型的错误。请参阅http://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice – user2314737 2014-12-11 11:24:49

回答

0

您可以使用正则表达式分割字符串,然后以“扁平化”的名单,以避免引号这样的分组:

替代

writer.writerow(away_game_stats) 

away_game_stats = [re.split(r"-|:",x) for x in away_game_stats] 
writer.writerow([x for y in away_game_stats for x in y]) 

(和相同)

0
import re 
print re.sub(r"-|:",",",test_string) 

观看演示。

https://regex101.com/r/aQ3zJ3/2

+0

我用writer.writerow([re.sub(r“ - |:',',' ,s)for s in home_game_stats]),它消除了冒号和连字符,但现在该项目被引号分组,使其仍然是csv文件中的一个项目,而不是两个单独的项目。 – 2014-12-07 16:24:32

+0

@ J.T。像GB,16,19,255,1,26:40,19,13,4,2,6-12,0-1,255,57,4.5,80,21,3.8,175,23- 33,4.9,3,14,1,4,38.3,8,65,1,0'不在单个物品上。 – vks 2014-12-07 16:28:44