[Regular Expression] How to Get Sentences Containing Specific Words in Python
I wrote a program that outputs only entire sentences containing specific words to the terminal when those words are found in Amazon Marketplace product condition comments.
This time, I took the case containing the word ‘新品’ (brand new) as an example from [Python] Tried Getting Product Condition Comments from Amazon API.
First, import the re module to use regular expressions.
import re
Next, use the re.search method to output the contents of conditionnote only when it contains the specific word ‘新品’ (brand new).
# Extract only conditionnote element information from soup
for item in soup.findAll("conditionnote"):
# When conditionnote element contains the word '新品', condition is true
if re.search(u'新品', item.contents[0]):
print item.contents[0] # Output contents of conditionnote element under item element
As shown below, only cases where condition comments contain ‘新品’ (brand new) are output.
Brand new unread item. Stock guaranteed. Will ship promptly. This is a brand new unread book. Carefully packed with waterproofing and shipped promptly. (Within 3 business days in principle) Orders will be promptly canceled if out of stock. Prices may be higher than retail, so please check the retail price. (Closed on Saturdays, Sundays, and holidays in principle.) Brand new unread item, so it's very clean. We respond promptly and carefully. International shipping available. Brand new beautiful item. Vinyl packed and shipped promptly via mail. (Depending on when you order, shipping is next day to one week) Please understand if out of stock. Rare item, so price is slightly higher than retail, but please consider. Treated as practically new item, sealed in vinyl immediately after purchase and kept unread. However, since it was once shelved or redistributed, condition is listed as "Very Good" for this listing. Please excuse any scuffs from display or shipping. Price is set higher than reference price (4830 yen close to retail) according to market conditions. Please consider carefully before purchasing. Inventory is shared so may be out of stock. In that case, orders will be canceled but please understand. Please order only if you don't mind stock shortages and high pricing.
That’s all.
That’s all from the Gemba.