Index in a list

Discussion in 'Python' started by Anatoli Hristov, Oct 17, 2012.

  1. Hello,

    I'm trying to index a text in a list as I'm importing a log file and
    each line is a list.

    What I'm trying to do is find the right line which contains the text
    User : and take the username right after the text "User :", but the
    list.index("(User :") is indexing only if all the text matching. How
    can I have the right position of the line which contains the word
    ("(User :")

    Thanks

    Anatoli
     
    Anatoli Hristov, Oct 17, 2012
    #1
    1. Advertising

  2. Anatoli Hristov

    Hans Mulder Guest

    On 17/10/12 12:10:56, Anatoli Hristov wrote:
    > I'm trying to index a text in a list as I'm importing a log file and
    > each line is a list.
    >
    > What I'm trying to do is find the right line which contains the text
    > User : and take the username right after the text "User :", but the
    > list.index("(User :") is indexing only if all the text matching. How
    > can I have the right position of the line which contains the word
    > ("(User :")


    Perhaps something like:

    target = "(User :"
    for line in your_list:
    position = line.find(target)
    if position >= 0:
    print line[position+len(target):]


    Hope this helps,

    -- HansM
     
    Hans Mulder, Oct 17, 2012
    #2
    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. karthikeyavenkat
    Replies:
    2
    Views:
    628
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    298
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    187
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    321
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    199
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page