0

I've got this code. This code removes the stopwords(in the stopwords.py file) from yelp.py

def remove_stop(text, stopwords):
    disallowed = set(stopwords)
    return [word for word in text if word not in disallowed]
text = open('yelp.py','r').read().split()
stopwords = open('stopwords.py','r').read().split()
print(remove_stop(text, stopwords))

Currently, the output is a very long string. I want the output to skip a line after every word in the yelp.py file. How do i do that? Can somebody help pls!!

The current output is ['near', 'best', "I've", 'ever', 'price', 'good', 'deal.', 'For', 'less', '6', 'dollars', 'person', 'get', 'pizza', 'salad', 'want.', 'If', 'looking', 'super', 'high', 'quality', 'pizza', "I'd", 'recommend', 'going', 'elsewhere', 'looking', 'decent', 'pizza', 'great', 'price,', 'go', 'here.']

How do i get it to skip a line?

Amanda Teo
  • 11
  • 2

2 Answers2

1

Once you have collected your output, a list l, you can print it as

print(*l, sep="\n")

where the * operator unpacks the list. Each element is used as a separate argument to the function.
Moreover, with the sep named argument you can customize the separator between items.

Full updated code:

def remove_stop(text, stopwords):
    disallowed = set(stopwords)
    return [word for word in text if word not in disallowed]

text = open('yelp.py','r').read().split()
stopwords = open('stopwords.py','r').read().split()
output = remove_stop(text, stopwords)
print(*output, sep="\n")
abc
  • 11,579
  • 2
  • 26
  • 51
  • Thank you for helping! It works. I ran the code in powershell. However, I realised that yelp.py still has the stopwords. How do I remove the stopwords in stopword.py from yelp.py? – Amanda Teo Aug 24 '18 at 09:35
0

When you print a list you get one long list as ouput [0, 1, 2, 3, 4, 5, ...]. Instead of printing the list you could iterate over it:

for e in my_list:
    print(e)

and you will get a newline after each element in the list.

Moberg
  • 5,253
  • 4
  • 38
  • 54