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.

1
  • import just means "I want to use this really useful thing in the library of cool stuff". Commented Sep 23, 2016 at 23:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
@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.
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.
@KennyOstrom OK. If you are inspired to write a good answer for the OP, I'll be happy to upvote it.
Maybe I just propose an edit with that one fix. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.