problem with string

Discussion in 'Python' started by Tzanko Tzanev, Jul 22, 2005.

  1. hi :)
    I need some help for this script
    I have
    --------------------
    cursor = conn.cursor()
    cursor.execute("select * from playlist limit 5")
    result = cursor.fetchall()
    # iterate through resultset
    playlist_txt = ''
    for record in result:
    mp3id = record[0]
    mp3_title = record[1]
    mp3_artist = record[2]
    playlist_txt += mp3id + mp3_title + mp3_artist
    #print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
    cursor.close()
    conn.close()
    ------------------
    #and want to print this out of "for record in result:"
    print playlist_txt

    #but there is an error in
    playlist_txt += mp3id + mp3_title + mp3_artist

    10x in advance :)
     
    Tzanko Tzanev, Jul 22, 2005
    #1
    1. Advertising

  2. Tzanko Tzanev wrote:

    > #but there is an error in
    > playlist_txt += mp3id + mp3_title + mp3_artist


    It'd be a lot easier to help if you'd say what the error was.
    --
    Michael Hoffman
     
    Michael Hoffman, Jul 22, 2005
    #2
    1. Advertising

  3. Tzanko Tzanev

    John Machin Guest

    Tzanko Tzanev wrote:
    > hi :)
    > I need some help for this script
    > I have
    > --------------------
    > cursor = conn.cursor()
    > cursor.execute("select * from playlist limit 5")
    > result = cursor.fetchall()
    > # iterate through resultset
    > playlist_txt = ''
    > for record in result:
    > mp3id = record[0]
    > mp3_title = record[1]
    > mp3_artist = record[2]
    > playlist_txt += mp3id + mp3_title + mp3_artist
    > #print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
    > cursor.close()
    > conn.close()
    > ------------------
    > #and want to print this out of "for record in result:"
    > print playlist_txt
    >
    > #but there is an error in
    > playlist_txt += mp3id + mp3_title + mp3_artist


    Have you considered doing "print record" at the appropriate place?

    Care to tell us what the error message is, or is it a state secret that
    can be obtained only by hanging you by your extremities and inserting
    laxatives into your ears with firehoses?
     
    John Machin, Jul 22, 2005
    #3
  4. Tzanko Tzanev a écrit :
    > hi :)
    > I need some help for this script
    > I have


    please take care of indentation when posting code.

    > --------------------
    > cursor = conn.cursor()
    > cursor.execute("select * from playlist limit 5")
    > result = cursor.fetchall()
    > # iterate through resultset
    > playlist_txt = ''
    > for record in result:

    mp3id = record[0]
    mp3_title = record[1]
    mp3_artist = record[2]
    playlist_txt += mp3id + mp3_title + mp3_artist

    Err... what about:
    print "<br />\n".join([" - ".join(record[:3]) for record in result])

    or :
    for record in result:
    print "%s - %s - %s <br />" % tuple(record[:3])

    Anyway, avoid string concatenation in loop, prefer appending to a list
    and then str.join():

    playlist = []
    for record in result:
    playlist.append(record[0] + record[1] + record[2])
    print "\n".join(playlist)

    Note that this is a very strange and complicated way to write:
    print "\n".join([''.join(rec) for rec[:3] in result])

    And, finally, if you just want to check what's in your resultset:
    print result

    > #print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
    > cursor.close()
    > conn.close()


    Since you're doing a fetchall(), you could (and should if you don't use
    em again) free your resources as soon as you've retrieved the resultset:

    cursor = conn.cursor()
    cursor.execute("select * from playlist limit 5")
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    # now use the resultset...


    > ------------------
    > #and want to print this out of "for record in result:"
    > print playlist_txt
    >
    > #but there is an error in
    > playlist_txt += mp3id + mp3_title + mp3_artist


    Are we supposed to guess what the error is ? Sorry, I'm not psychic
    enough. Please post the full traceback.

    (wild guess: mp3id is an integer ?)
     
    Bruno Desthuilliers, Jul 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    755
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,682
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    531
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,985
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,808
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page