【发布时间】:2018-02-20 00:05:27
【问题描述】:
我正在尝试解析节目时间表,以便确定某些节目的播出时间。我在解析整个字符串时创建了一个相对有效的程序,但是当我将它分成几行时,它开始生成字符串索引超出范围错误。我通过将范围减少 1 并确保我没有检查不存在但现在程序只打印两次的索引坐标来解决这个问题。这是我的代码。
scheduleLines = schedule.splitlines()
timeCoordinate = -10
for Line in scheduleLines:
for counter in range(len(Line) - 1):
if counter <= timeCoordinate:
continue
if Line[counter].isdigit() == True:
if Line[counter + 1] == ":":
print(Line[counter:counter + 4])
timeCoordinate = counter + 4
if counter + 2 < len(Line):
if Line[counter + 2] == ":":
print(Line[counter:counter + 5])
timeCoordinate = counter + 5
它只打印 9:00 和 8:00,这是时间表。该程序的先前版本可以随时提取和打印。当我将时间表发布到 StackoverFlow 时,我无法正确格式化时间表,但每次加上节目名称都是不同的行。
-
2 月 12 日星期一
上午 9:00 - CanDo 的第 9 集
9:30 - 故事时间 ep7
10:00am Afro Tusk ep3
11:00a - Thunderbolt ep1
12:00 - 西雅图此时此地 ep27
1:00p - Startime ep53
3:00p - heartBeat Radio ep6
3:30 单身父亲 ep13p
4:00p - 堵车
6:00 - 尽情享受第 7 集
6:30 运动与运动 ep15
7:00 - Lidline 运动
8:00 - 西雅图体育周刊 ep22
9:00p - 雷鬼 ep6
10:00 - MoJam 星期一第 7 集
11:00 只是我的意见 ep21
午夜 - 多元化编程
【问题讨论】:
-
你能正确格式化你的样本数据吗?
-
我试过了,但我不知道怎么做。
标签: python arrays string split range