Hi all

I have got a problem with the list of strings. I have got the full list of strings, but I can't be able to print for each string which I can only print for the full string.

When I use this:

programList = list()

# set the channels text
for index in range(0, CHANNELS_PER_PAGE):

    #get the programs list
    cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel=?', [channel])
    programs = cur.fetchall()

    for row in programs:
        title = row[1].encode('ascii')

        if program_width > 1:
            programList.append(title)
programTitle = programList
print programTitle

I will get the results like this:

21:17:50 T:5172  NOTICE: ['The Middle -  The Ditch', 'The Goonies', 'Pirates of the Caribbean: On Stranger Tides', 'The 700 Club', 'The Fresh Prince of Bel-Air -  Day Damn One', 'The Fresh Prince of Bel-Air -  Lucky Charm', 'The Fresh Prince of Bel-Air -  The Ethnic Tip', 'The Fresh Prince of Bel-Air -  The Young and the Restless', 'Summer Sexy With T25!', 'Paid Programming', 'The 700 Club', "Shaun T's Focus T25", 'Sleep Better!', 'Joseph Prince', 'Life Today With James Robison -  Christine Caine -  FOY, MOR 1', 'Joyce Meyer: Enjoying Everyday Life', 'Amazing Facts Presents', "That '70s Show -  Who Wants It More?", "That '70s Show -  Fez Gets the Girl", "That '70s Show -  Dine and Dash", "That '70s Show -  Radio Daze", '700 Club Special Programming', 'Gilmore Girls -  A Deep-Fried Korean Thanksgiving', '8 Simple Rules -  Princetown Girl', '8 Simple Rules -  A Very C.J. Christmas', 'Reba -  And the Grammy Goes To ...', 'Reba -  The Wall', 'Reba -  The Best Defense', 'Reba -  For Sale, Cheap', 'Boy Meets World -  State of the Unions', 'Boy Meets World -  Show Me the Love', 'Boy Meets World -  For Love and Apartments', "Boy Meets World -  Angela's Men", 'The Middle -  The Cheerleader', 'The Middle -  The Block Party', 'The Middle -  The Floating Anniversary', 'The Middle -  The Trip', 'Melissa & Joey -  Right Time, Right Place', "Melissa & Joey -  Don't Look Back in Anger", 'Melissa & Joey -  Accidents Will Happen', 'Baby Daddy -  Send in the Clowns', 'Liar Liar', 'The 700 Club', 'Baby Daddy -  Flirty Dancing', 'Baby Daddy -  Send in the Clowns'
..etc

I want to make the results like this:

21:20:01 T:5796  NOTICE: The Middle -  The Ditch
21:20:01 T:5796  NOTICE: The Goonies
21:20:01 T:5796  NOTICE: Pirates of the Caribbean: On Stranger Tides
21:20:01 T:5796  NOTICE: The 700 Club
21:20:01 T:5796  NOTICE: The Fresh Prince of Bel-Air -  Day Damn One
21:20:01 T:5796  NOTICE: The Fresh Prince of Bel-Air -  Lucky Charm
21:20:01 T:5796  NOTICE: The Fresh Prince of Bel-Air -  The Ethnic Tip
21:20:01 T:5796  NOTICE: The Fresh Prince of Bel-Air -  The Young and the Restless
21:20:01 T:5796  NOTICE: Summer Sexy With T25!
21:20:01 T:5796  NOTICE: Paid Programming
21:20:01 T:5796  NOTICE: The 700 Club
21:20:01 T:5796  NOTICE: Shaun T's Focus T25
21:20:01 T:5796  NOTICE: Sleep Better!
21:20:01 T:5796  NOTICE: Joseph Prince
21:20:01 T:5796  NOTICE: Life Today With James Robison -  Christine Caine -  FOY, MOR 1
21:20:01 T:5796  NOTICE: Joyce Meyer: Enjoying Everyday Life
21:20:01 T:5796  NOTICE: Amazing Facts Presents
21:20:01 T:5796  NOTICE: That '70s Show -  Who Wants It More?
21:20:01 T:5796  NOTICE: That '70s Show -  Fez Gets the Girl
21:20:01 T:5796  NOTICE: That '70s Show -  Dine and Dash
21:20:01 T:5796  NOTICE: That '70s Show -  Radio Daze
21:20:01 T:5796  NOTICE: 700 Club Special Programming
21:20:01 T:5796  NOTICE: Gilmore Girls -  A Deep-Fried Korean Thanksgiving
21:20:01 T:5796  NOTICE: 8 Simple Rules -  Princetown Girl
21:20:01 T:5796  NOTICE: 8 Simple Rules -  A Very C.J. Christmas
21:20:01 T:5796  NOTICE: Reba -  And the Grammy Goes To ...
21:20:01 T:5796  NOTICE: Reba -  The Wall
21:20:01 T:5796  NOTICE: Reba -  The Best Defense
21:20:01 T:5796  NOTICE: Reba -  For Sale, Cheap
21:20:01 T:5796  NOTICE: Boy Meets World -  State of the Unions
21:20:01 T:5796  NOTICE: Boy Meets World -  Show Me the Love
21:20:01 T:5796  NOTICE: Boy Meets World -  For Love and Apartments
21:20:01 T:5796  NOTICE: Boy Meets World -  Angela's Men
21:20:01 T:5796  NOTICE: The Middle -  The Cheerleader
21:20:01 T:5796  NOTICE: The Middle -  The Block Party
21:20:01 T:5796  NOTICE: The Middle -  The Floating Anniversary
21:20:01 T:5796  NOTICE: The Middle -  The Trip
21:20:01 T:5796  NOTICE: Melissa & Joey -  Right Time, Right Place
21:20:01 T:5796  NOTICE: Melissa & Joey -  Don't Look Back in Anger
21:20:01 T:5796  NOTICE: Melissa & Joey -  Accidents Will Happen
21:20:01 T:5796  NOTICE: Baby Daddy -  Send in the Clowns

Do you know how I can print for each string to allow me to make the results that I actually want?

You can try:

pre = "21:20:01 T:5796  NOTICE:"
for item in programList:
    print("{} {}".format(pre, item))

Thanks sneekula, but I want to print for each title outside of the loop.

How I can do that?

I want to print for each title outside of the loop.

This is meaningless. It does not matter if the titles are printed inside or outside of the loop (which loop ? why ?) as long as the program prints the correct output. Change your priorities: first the program must work as expected, then the secondary details.

Oh ok thank you for your advice.

I would like you to help me with this:

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

for index in range(0, CHANNELS_PER_PAGE):
    channel = channelList[index]

    print channel
    #get the programs list
    cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel=?', [channel])
    programs = cur.fetchall()
    start_pos = 368    # indent for first program
    for row in programs:
        title = row[1].encode('ascii')

If I use the code on above to print channel in the for index in range loop, i will get this:

23:32:03 T:1164  NOTICE: 101 ABC FAMILY
23:32:03 T:1164  NOTICE: 102 CBS
23:32:03 T:1164  NOTICE: 103 CNN USA
23:32:03 T:1164  NOTICE: 105 ESPN USA
23:32:03 T:1164  NOTICE: 106 Fox News
23:32:03 T:1164  NOTICE: 107 Animal Planet
23:32:03 T:1164  NOTICE: 108 USA Network

So when I try this:

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

for index in range(0, CHANNELS_PER_PAGE):
    channel = channelList[index]

print channel
#get the programs list
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel=?', [channel])
programs = cur.fetchall()
start_pos = 368    # indent for first program
for row in programs:
    title = row[1].encode('ascii')

If I use the print channel outside of the loop, i will get this:

23:30:09 T:5828  NOTICE: 108 USA Network

I want to put the loop for row in programs: outside of the for index in range loop in prevent my code keep firing up.

How do you use to get the list of channels to allow me to print for each channel outside of the for index in range loop?

I suggest that you call the following function as soon as you get a cursor object:

from collections import namedtuple
from operator import attrgetter
import itertools

def load_programs(cur):
    """Return a dictionary channel -> list of program records"""
    tp = namedtuple('record', 'channel title start_date stop_date')
    cur.execute("""SELECT channel, title, start_date, stop_date FROM programs""")
    L = [tp(row[0].encode('ascii'),row[1].encode('ascii'), row[2], row[3])
            for row in cur.fetchall()]
    L = sorted(L, key=attrgetter('channel'))
    result = dict()
    for channel, group in itertools.groupby(L, key=attrgetter('channel')):
        result[channel] = list(group)
    return result

This will get you a dictionary object mapping each channel to the list of programs for this channel. Then you do what you want with this dictionary. For example try this

from pprint import pprint
...
programdict = load_programs(cur)
pprint(programdict)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.