如何把弦分开

时间:2014-07-22 15:54:25

标签: python python-2.7

我正在使用我的python脚本从sqlite3数据库中获取通道列表。

当我获取频道列表以设置每个标签控件40104011401240134014,{{ 1}}和4015,它会将字符串拆分为每个字母,它将放置每个字符串4016101,{{ 1}},A在每个标签控件中。

我想要实现的是获取通道列表以设置每个标签控件中的每个通道而不拆分字符串。

当我使用它时:

B

以下是我打印时的频道列表:

C

我想在每个标签控件中设置文本,如下所示:

#Pull the data from the database
channelList = list()
programList = list()
database_path = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))

if os.path.exists(database_path):
    #get the channels list
    cur.execute('SELECT channel FROM programs WHERE channel GROUP BY channel')


    for row in cur:
        channel = row[0].encode('ascii')
        channelList.append(channel)

        # set the channels text
        for index in range(0, channels_per_page):
            self.getControl(4110 + index).setLabel(channels[index])

结果如下:

17:02:10 T:6400  NOTICE: 101 ABC FAMILY
17:02:10 T:6400  NOTICE: 102 CBS
17:02:10 T:6400  NOTICE: 103 CNN USA
17:02:10 T:6400  NOTICE: 105 ESPN USA
17:02:10 T:6400  NOTICE: 106 Fox News
17:02:10 T:6400  NOTICE: 107 Animal Planet
17:02:10 T:6400  NOTICE: 108 USA Network
17:02:10 T:6400  NOTICE: 110 SPIKE
17:02:10 T:6400  NOTICE: 111 BRAVO USA
17:02:10 T:6400  NOTICE: 112 BRAVO1
17:02:10 T:6400  NOTICE: 113 BRAVO2
17:02:10 T:6400  NOTICE: 114 BRAVO3
17:02:10 T:6400  NOTICE: 115 BRAVO4
17:02:10 T:6400  NOTICE: 116 BRAVO5
17:02:10 T:6400  NOTICE: 117 BRAVO6
17:02:10 T:6400  NOTICE: 118 BRAVO7

1 个答案:

答案 0 :(得分:0)

我知道这可能是一个可怕的词,但regex是完美的。

import re
index = 4110
# Assuming there a line that is for example 17:02:10 T:6400  NOTICE: 101 ABC FAMILY
regexString = '\S+ \s+ \S+ \s+ \S+ \s+ (?P<show>.*)'
regex       = re.compile(regexString, re.X)
for line in lines:
    match = regex.match(line)
    print index, match.group('show')
    index += 1