Python list for different number of strings per line

asteroid4u

I have output file like below:

junk
abc 123 xyz test
123 abc test 123
bob ani kepu maro
exist

What i am trying with this file is I am searching kepu is present in column 3 in the file.

I am trying below code:

   name = "kepu"
   with open("listfile.txt") as f:
      for line in f:
         line = line.split(" ")[2]
         line = line.lower()
         if line == name:
            print "Present"
         else:
            print "Not Present"

Since in file does not have equal number of strings, In first line "junk" and last line "exist" contains only one string and trying

line = line.split(" ")[2] 

This is the reason i am getting below error:

 IndexError: list index out of range

I tried removing first and last line that is junk and exist keyword it works. but my output file generation is random.

Please help me what is the best way to find the string is exit in the third column in file.

Phung Duy Phong

Or you can throw catch an exception:

name = "kepu"
with open("listfile.txt") as f:
for line in f:
    try:
        line = line.split(" ")[2]
    except IndexError:
        continue
    line = line.lower()
    if line == name:
        print "Present"
    else:
        print "Not Present"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python count number of strings in a list of variable types, without loop if possible

python list to 'n' number of strings depending on length of list

Number of clients on different waiting list per year-month

Python replace empty strings in a list with values from a different list

Add number to the strings in the list in python

Search a list to see if it contains strings stored in a different list in python

Python: Find and Print strings, multiple instances per line

List a number of copies per book

Python plot a list of lists with different number of elements

Inserting blank line between strings that starts with different number

Python - Count number of words in a list strings

how to split a list in to different strings in python

List per Date python

Replacing same keyword with different strings per line

Python: Comparing strings in a list with two different list lengths

Limiting the number of characters per line

Number of different values per column in a split list

Different number of rows for different columns per page

Python List to Pandas DataFrame with Number & Strings

How can I print strings in a list by the number in a different list, on different lines depending on a third list?

Python sort strings in a list of strings by 3 last characters and a number

Takes a list of strings and returns each line prepended by the correct number: Ruby

How to get the all the strings from a list of different tuples with text and number in python

JSON writes one list element per line | Python

Counting number of occurrence of different strings per line and appending counts as columns

Python: Finding strings in a list containing an interval and replacing this interval by every number in it

Python for loop list of strings different output

Delete characters on python dataframe, the number of characters removed per line varies

Variable with x number of strings from list in Python