James Dean James Dean - 5 months ago 34
Python Question

python script - grouping words into a If-Not statement

Trying to figure out how to use an if not statement in which I can group three to four words to omit from a CSV file. Towards the bottom of the code, you'll see that I'm stuck at:

if ('reddit', 'passwords') not in x:

Any help would be great.

# import libraries
import bs4
from urllib2 import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.reddit.com/r/NHLStreams/comments/71uhwi/game_thread_sabres_at_maple_leafs_730_pm_et/'

# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()

# html parsing
page_soup = soup(page_html, "html.parser")

filename = "sportstreams.csv"
f = open(filename, "w")
headers = "Sport Links " + "\n"

links = page_soup.select("form a[href]")
for link in links:
href = link["href"]

f.write(href + "\n")

with open('sportstreams.csv') as f,open('sstream.csv', "w") as f2:
for x in f:
if ('reddit', 'passwords') not in x: # trying to find multi words to omit

Answer Source

Use the builtin function all:

if all(t not in x for t in ('reddit', 'passwords')):

Or any:

if not any(t in x for t in ('reddit', 'passwords')):

Here's it is in your context manager:

with open('sportstreams.csv') as f, open('sstream.csv', "w") as f2:
    for line in f:
        if any(t in line for t in ('reddit', 'passwords')):
            # The line contains one of the strings.
            # The line contains none of the strings.
            f2.write(line.strip() + '\n')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download