从变量Python中提取字符串
在执行从变量Python中提取字符串,python,beautifulsoup,Python,Beautifulsoup,在执行''之后,我将类似的内容存储在一个变量中。将(soup.findAll(text=True))连接到一个经过解析的url,我必须根据给定的参数获取学校以及分数和它玩的人。。类似于test.py“norfolk st.”应该像“Norfolk St.0-38 Rutgers”那样获取分数……我尝试使用了几个函数,如re.search()、string.find()和解析无法获取预期结果的字符?需要帮助吗 Norfolk St. 0 - 38 Rutgers F
''之后,我将类似的内容存储在一个变量中。将(soup.findAll(text=True))
连接到一个经过解析的url,我必须根据给定的参数获取学校以及分数和它玩的人。。类似于test.py“norfolk st.”
应该像“Norfolk St.0-38 Rutgers”那样获取分数……我尝试使用了几个函数,如re.search()、string.find()和解析无法获取预期结果的字符?需要帮助吗
Norfolk St.
0 - 38
Rutgers
Final
South Florida
6 - 21
Michigan St.
Final
Chowan
7 - 47
Charlotte
Final
SE Louisiana
17 - 38
(24) TCU
Final
W. Kentucky
20 - 52
Tennessee
Final
S. Carolina St.
13 - 52
(4) Clemson
Final
Middle Tenn. St.
20 - 40
North Carolina
Final
Central Conn. St.
44 - 51
Lehigh
Final OT
Army
14 - 40
Ball St.
Final
问题是我必须从这个urlhttp://sports.yahoo.com/college-football/scoreboard/?conf=all
当用户在命令行参数中给出学校名称时,它必须转到此URI检查学校名称是否存在超链接,并且必须重定向并获取类似以下内容的方框分数这个
1 2 3 4 Total
FAU 3 3 0 7 13
ECU 7 14 10 0 31
如果游戏正在进行中,检索到的分数应该休眠指定的秒数,然后检索到最新的分数。所以我不确定我必须走哪条路!需要帮助,因为我是python新手。是结构[team 1][score][team 2][游戏说明]?例如[第一队]=“诺福克街”、[得分]=“0-38”、[第二队]=“罗格斯大学”、[游戏说明]=“决赛”在您最初的示例中 并且:您的目标是在命令行上输入一个团队来检索他们所玩的所有游戏的记录(两个团队和分数) 假设这两种情况:我首先尝试按行标记:
lines = your_string.split('\n').
clean_lines = [l.strip() for l in lines]
然后我会建立一个实际游戏的列表:
In [8]: games = [clean_lines[i:i+4] for i in xrange(0, len(clean_lines), 4)]
In [9]: games
Out[9]:
[['Norfolk St.', '0 - 38', 'Rutgers', 'Final'],
['South Florida', '6 - 21', 'Michigan St.', 'Final'],
['Chowan', '7 - 47', 'Charlotte', 'Final'],
['SE Louisiana', '17 - 38', '(24) TCU', 'Final'],
['W. Kentucky', '20 - 52', 'Tennessee', 'Final'],
['S. Carolina St.', '13 - 52', '(4) Clemson', 'Final'],
['Middle Tenn. St.', '20 - 40', 'North Carolina', 'Final'],
['Central Conn. St.', '44 - 51', 'Lehigh', 'Final OT'],
['Army', '14 - 40', 'Ball St.', 'Final']]
如果要查找给定团队参与的所有游戏,只需遍历游戏列表并检查该团队的字符串是否出现在0或2索引中。但是,如果要多次查找,最好建立一个字典,其中键是团队名称,值是他们所玩游戏的索引
希望有帮助 我不会为正则表达式操心。基于文本,它看起来像字符串,减去空格,大致遵循并重复以下格式:
thing 1
score
thing 2
"final"
因此,我可以继续,清理字符串,遍历它,并将每组4作为字典的一部分返回
例如:
def chunk(iterable, n):
'''chunk([1, 2, 3, 4, 5, 6], 2) -> [[1, 2], [3, 4], [5, 6]]'''
return [iterable[i:i+n] for i in range(0, len(iterable), n)]
def get_scores(raw):
clean = [line.strip() for line in raw.split('\n') if line.strip() != '']
return {thing1: (thing1, score, thing2) for (thing1, score, thing2, _) in chunk(clean, 4)}
然后,你可以做:
>>> raw = ''.join(soup.findAll(text=True))
>>> scores = get_scores(raw)
>>> print scores['Norfolk St.']
('Norfolk St.', '0 - 38', 'Rutgers')
def get_scores(raw):
clean = [line.strip().lower() for line in raw.split('\n') if line.strip() != '']
return {thing1: (thing1, score, thing2) for (thing1, score, thing2, _) in chunk(clean, 4)}
如果希望查找不区分大小写,可以执行以下操作:
>>> raw = ''.join(soup.findAll(text=True))
>>> scores = get_scores(raw)
>>> print scores['Norfolk St.']
('Norfolk St.', '0 - 38', 'Rutgers')
def get_scores(raw):
clean = [line.strip().lower() for line in raw.split('\n') if line.strip() != '']
return {thing1: (thing1, score, thing2) for (thing1, score, thing2, _) in chunk(clean, 4)}
如果您希望能够查找“诺福克街”或“罗格斯”并获得相同的结果,可以执行以下操作:
def get_scores(raw):
clean = [line.strip().lower() for line in raw.split('\n') if line.strip() != '']
output = {}
for (thing1, score, thing2, _) in chunk(clean, 4):
data = (thing1, score, thing2)
output[thing1] = data
output[thing2] = data
return output
模式是什么?我的意思是学校,点(?),竞争对手,等等。有了一个模式,也许你可以把它们分成一个数组,使解析更容易。Thanks@sl8r000,问题是我必须从这个url
http://sports.yahoo.com/college-football/scoreboard/?conf=all
当用户在命令行参数中给出学校名称时,它必须转到此URI检查学校名称是否存在超链接,并且必须重定向并获取类似于以下内容的方框分数1 2 3 4总计
FAU 3 3 0 7 13
ECU 7 14 10 0 31
如果游戏正在进行,则检索到的分数应休眠指定秒,然后检索到最新分数。因此我不确定我必须走哪条路!我需要帮助。