Changing phrases to vectors with while function in Python
<p>I would like to change the following phrases to vectors with sklearn:</p>

<pre><code>Article 1. It is not good to eat pizza after midnight
Article 2. I wouldn't survive a day withouth stackexchange
Article 3. All of these are just random phrases
Article 4. To prove if my experiment works.
Article 5. The red dog jumps over the lazy fox
</code></pre>

<p>I got the following code:</p>

<pre><code>from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=1)

n=0
while n &lt; 5:
   n = n + 1
   a = ('Article %(number)s' % {'number': n})
   print(a)
   with open("LISR2.txt") as openfile:
     for line in openfile:
       if a in line:
           X=line
           print(vectorizer.fit_transform(X))
</code></pre>

<p>Which gives me the following error:</p>

<pre><code>ValueError: Iterable over raw text documents expected, string object received.
</code></pre>

<p>Why does this happen? I know this should work because if I type in individually:</p>

<pre><code>X=("It is not good to eat pizza","I wouldn't survive a day", "All of these")

print(vectorizer.fit_transform(X))
</code></pre>

<p>It gives me my desired vectors.</p>

<pre><code>(0, 8)  1
(0, 2)  1
(0, 11) 1
(0, 3)  1
(0, 6)  1
(0, 4)  1
(0, 5)  1
(1, 1)  1
(1, 9)  1
(1, 12) 1
(2, 10) 1
(2, 7)  1
(2, 0)  1
</code></pre>