Harp Angell Harp Angell - 6 months ago 9
Python Question

Very new to python, looking for some clarification

When I use this function:

soup = BeautifulSoup(sock,'html.parser')
for string in soup.stripped_strings:
if string == "$":
pass
else:
print string


It prints out the following values, skipping the $:

the
cat
has
nine
lives


Is this the best way to do this if I want to save this info into a database?

In the end what I want is a table that has |the|cat|has|nine|lives|

Answer

You can index into strings as if they are arrays, so you could use string[0] == '$', or string.startswith(). e.g.

strings = ['$', 'the', '$big', 'cat']
for s in strings:
  if s[0] != '$':
    print(s)

for s in strings:
  if not s.startswith('$'):
    print(s)

You can also directly make a filtered list with list comprehensions like this:

nodollarstrings = [s for s in strings if not s.startswith('$')]
Comments