Ankita
Ankita

Reputation: 485

Python: selecting sentence based on given words and export them as excel

let say, I have the following paragraph as string:

str=You can also use negative index numbers to slice a string. As we went through before, negative index numbers of a string start at -1, and count down from there until we reach the beginning of the string. When using negative index numbers, we’ll start with the lower number first as it occurs earlier in the string.

I want to find a sentence that contains the word input words= 'negative' and 'string'. And list them in separate str of each sentence. For example, I am expected to get output

negative= a) You can also use negative index numbers to slice a string.
          b ) As we went through before, negative index numbers of a string start at -1, and count down from there until we reach the beginning of the string. 
          c) When using negative index numbers, we’ll start with the lower number first as it occurs earlier in the string.
string= a) You can also use negative index numbers to slice a string
        b) As we went through before, negative index numbers of a string start at -1, and count down from there until we reach the beginning of the string.
        c) When using negative index numbers, we’ll start with the lower number first as it occurs earlier in the string.

As you can see all sentences contains both 'negative' and 'string' words, hence includes all sentences.

If there is any way to do this without using for loop or in a simple way?

Upvotes: 1

Views: 158

Answers (1)

Antoine Delia
Antoine Delia

Reputation: 1845

I would suggest splitting your string to get each sentence first. Then, check if the words are contained in each sentence.

To store each list into an Excel file, you will need to install pandas first.

$ pip install pandas

You might also need to install openpyxl.

$ pip install openpyxl

Here's how you can achieve this result:

import pandas as pd

huge_sentence = "You can also use negative index numbers to slice a string. As we went through before, negative index numbers of a string start at -1, and count down from there until we reach the beginning of the string. When using negative index numbers, we’ll start with the lower number first as it occurs earlier in the string."

sentences = huge_sentence.split(".")

sentence_with_negative_word = [sentence + "." for sentence in sentences if "negative" in sentence]
sentence_with_string_word = [sentence + "." for sentence in sentences if "string" in sentence]

# Now, we export each list in a different Excel file
pd.DataFrame(sentence_with_negative_word).to_excel('negative.xlsx', header=False, index=False)
pd.DataFrame(sentence_with_string_word).to_excel('string.xlsx', header=False, index=False)

Upvotes: 2

Related Questions