-1

I have Excel files with data that I need to summarize so I've created a list called a_master that contains all of this info (using xlrd).

What I need to do now is search for a specific word for example "cloud" and if it finds the word, to print out the sentence that contains that word. It doesn't seem to work though.

It outputs everything that it finds after the searchable word.

for line in str(a_master):
  if "cloud" in line:
    print line
bladexeon
  • 696
  • 3
  • 10
  • 31
Danny
  • 75
  • 1
  • 2
  • 8
  • 6
    Did you mean `if "cloud" in line:`? Also, `for line in str(a_master)` iterates all the _characters_ in the string representaiton, not the lines. – tobias_k Apr 23 '15 at 14:44
  • 1
    If you use `str` and then iterate you'll take one letter at a time. – Peter Wood Apr 23 '15 at 14:45
  • What exactly is the format of a single element in the `a_masters` list? Please show some example input and output. – tobias_k Apr 23 '15 at 14:46
  • @tobias_k Sorry, yes that's my mistake. Fixed it in the question. That doesn't work either fyi – Danny Apr 23 '15 at 14:46
  • @tobias_k It's a load of sentences. with a few \u and " ", thrown in when their are spaces in the excel files – Danny Apr 23 '15 at 14:49
  • "for line in str(a_master)" You're turning all of a_master into a string? That would just mean you iterate over each character, not a line of the file. If a_master is a list of strings you can just leave out the str() function. – SuperBiasedMan Apr 23 '15 at 14:50

1 Answers1

2

If a_master is a list as you said, probably this is what you want:

for line in a_master:
  str_line = str(line)
  if "cloud" in str_line:
    print str_line
Luca
  • 322
  • 1
  • 13