1

I'm trying to write a function where the input has a keyword that occurs multiple times in a string and will print the stuff that has double quotation marks between them after the keyword. Essentially...

Input= 'alkfjjiekeyword "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee keyword"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness' 

Output= someonehelpmepls 

itonlygivesmeoneinsteadofmultiple 

If its possible to have the outputs as its own line that would be better.

Here's what I have so far:

def getEm(s): 

    h = s.find('keyword') 

    if h == -1 
       return -1 

    else: 
       begin = s.find('"',h) 
       end = s.find('"', begin+1) 
       result = s[begin +1:end] 
    print (result)

Please don't suggest import. I do not know how to do that nor know what it is, I am a beginner.

Community
  • 1
  • 1
Anymee
  • 121
  • 1
  • 12

1 Answers1

2

Let's take some sample input:

>>> Input= 'alkfjjiekeyword "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee keyword"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness'

I believe that one " was missing from the sample input, so I added it.

As I understand it, you want to get the strings in double-quotes that follow the word keyword. If that is the case, then:

def get_quoted_after_keyword(input):
    results = []
    split_by_keyword = input.split('keyword')
    # you said no results before the keyword
    for s in split_by_keyword[1:]:
        split_by_quote = s.split('"')
        if len(split_by_quote) > 1:
            # assuming you want exactly one quoted result per keyword
            results.append(split_by_quote[1])
    return results

>print('\n'.join(get_quoted_after_keyword(Input))
>someonehelpmepls
>itonlygivesmeoneinsteadofmultiple

How it works

Let's look at the first piece:

>>> Input.split('keyword')
['alkfjjie',
 ' "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee ',
 '"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness']

By splitting Input on keyword, we get, in this case, three strings. The second string to the last are all strings that follow the word keyword. To get those strings without the first string, we use subscripting:

>>> Input.split('keyword')[1:]
[' "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee ',
 '"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness']

Now, our next task is to get the part of these strings that is in double-quotes. To do that, we split each of these strings on ". The second string, the one numbered 1, will be the string in double quotes. As a simpler example, let's take these strings:

>>> [s.split('"')[1] for s in ('"one"otherstuff', ' "two"morestuff')]
['one', 'two']

Next, we put these two steps together:

>>> [s.split('"')[1] for s in Input.split('keyword')[1:]]
['someonehelpmepls', 'itonlygivesmeoneinsteadofmultiple']

We now have the strings that we want. The last step is to print them out nicely, one per line:

>>> print('\n'.join(s.split('"')[1] for s in Input.split('keyword')[1:]))
someonehelpmepls
itonlygivesmeoneinsteadofmultiple

Limitation: this approach assumes that keyword never appears inside the double-quoted strings.

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Looks promising, but you can get list index out of range pretty easily, and you assume the correct answer is to give one quoted result per keyword. – Kenny Ostrom Sep 24 '16 at 00:18
  • @KennyOstrom Yes, there are many hypotheticals but, without a clear problem statement, we can only guess at the correct code. Should it print all double-quotes strings? Or, should a double-quoted string not preceded by `keyword` be considered an error? Or, should such double-quoted strings be ignored? The possibilities are many. – John1024 Sep 24 '16 at 00:25
  • That's fair. But the list index out of range still bothers me. You could split them on separate lines, have easier code for a newbie to read, and have a temp variable to check for len. – Kenny Ostrom Sep 24 '16 at 00:39
  • @KennyOstrom OK. If you are inspired to write a good answer for the OP, I'll be happy to upvote it. – John1024 Sep 24 '16 at 01:32
  • Maybe I just propose an edit with that one fix. :) – Kenny Ostrom Sep 24 '16 at 01:40
  • Thank you everyone! Can someone explain what '\n' does? I have never seen this. – Anymee Sep 24 '16 at 23:42
  • In Python, `\n` is the newline character: it marks the end of a line. – John1024 Sep 25 '16 at 01:41