python - Printing the line in a text file that contains a string -


I am trying to get a line of text file that has a fixed string and printing the third number or string Is the line. The text file looks like this:

  1997 180 60 Bob 1997 145 59 Dan  

If input text contains bob So my code should print to 60 .

Even how far away I have:

  calWeight = [line in open ('user_details.txt') for row if string in name] stringCalWyight = Str (calWeight) Print (StringCalVight)  

How can I fix this?

If you want a list of all the points where the bob is in line, then the understanding of the list Use:

  with open ('user_details.txt') as F: nums = [line.split () [2] in the line f in the "Bob" line ]  

You want to break even before you check whether you want to avoid the case. The name line is the substrings of a string, for example bob in bobbing -> true:

  nums = [line.split () [2] line In f "Bob" line.split ()] I  

I think that a more useful structure would be a dict. There are all the third numbers in a row connected to each name: / P> from

  import as default ("in.txt") with the default decode d = defaultdict (list) f: for line F: if line.strip (): spl = line Rstrip () Split () d [spl [-1]] Epindy (spl [2]) print (D) default dict (& lt; type 'list' & gt ;, '' Bob ': [' 60 '],' donation ' : ['59']})  

Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -