如何打印字符串列表?

时间:2014-11-18 18:20:23

标签: python

我正在处理我的python脚本以获取sqlite3数据库中的字符串列表。当我从sqlite3数据库中提取字符串时,我无法打印每个字符串,因为我只能打印循环外的完整字符串列表。

当我使用它时:

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

我会得到这样的结果:

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

我想得到这样的结果:

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

你能告诉我一个示例代码如何打印循环外的每个字符串以获得我真正想要的结果吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

print '\n'.join(programTitle)

那应打印每个标题后跟换行符(\n)。

由于Python的join语法有点奇怪,请参阅以下帖子:

Python join: why is it string.join(list) instead of list.join(string)?