简体   繁体   English

如何打印出字符串列表?

[英]How To Print Out A List Of Strings?

I'm working on my python script to get the list of strings from the sqlite3 database. 我正在使用python脚本从sqlite3数据库中获取字符串列表。 When I pulled the strings from the sqlite3 database, I can't be able to print for each string as I can only print the full list of strings outside of the loop. 当我从sqlite3数据库中提取字符串时,我无法为每个字符串打印,因为我只能在循环外部打印完整的字符串列表。

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

Can you please show me an example code how I can print for each strings outside of the loop to get the results that I actually want? 您能给我看一个示例代码,我如何在循环外为每个字符串打印以获得我想要的结果?

Try this: 尝试这个:

print '\n'.join(programTitle)

That should print each title followed by a newline ( \\n ) character. 那应该打印每个标题,后跟一个换行符( \\n )。

Since Python's join syntax is a little bit weird, see the following post: 由于Python的join语法有点怪异,请参见以下文章:

Python join: why is it string.join(list) instead of list.join(string)? Python join:为什么用string.join(list)代替list.join(string)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM