Index in a list

Hans Mulder hansmu at xs4all.nl
Wed Oct 17 12:46:28 EDT 2012


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




More information about the Python-list mailing list