Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Index in a list

Reply
Thread Tools

Index in a list

 
 
Anatoli Hristov
Guest
Posts: n/a
 
      10-17-2012
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
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      10-17-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List index out of range, but list has enough elements Costin GamenČ› Python 1 11-09-2010 09:37 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
difference between `x in list` and `list.index(x)` for instances of anew-style class Riccardo Murri Python 3 12-30-2007 01:55 PM
List Util insert_at(index, value), delete_at(index) ngoc Perl Misc 5 05-11-2006 12:31 PM
List index method for complex list item types? techiepundit@futurepundit.com Python 4 12-31-2005 03:44 AM



Advertisments