1 python split string-split removing-whitespace
我正在分裂并删除像这样的空白区域.
team = "Hisingsbacka - Guldhedens IK"
homeTeam, awayTeam = team.replace(' ','').split("-")
Run Code Online (Sandbox Code Playgroud)
如果我打印出来,它会显示:
homeTeam = "Hisingsbacka" <-- This one is ok for this case
awayteam = "GuldhedensIK" <-- not this one, space between the words needed as shown below
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
homeTeam = "Hisingsbacka"
awayteam = "Guldhedens IK"
Run Code Online (Sandbox Code Playgroud)
请注意我有几个从网上解析的蜇,其中一些具有相同的"样式/格式"或任何你称之为awayTeam的意思是"word1 word2"因此有时双方都会有这种格式,有时只有右侧,有时只有左侧.
不要删除空格; 分裂后使用str.strip()结果:
team = "Hisingsbacka - Guldhedens IK"
homeTeam, awayTeam = (t.strip() for t in team.split("-"))
Run Code Online (Sandbox Code Playgroud)