In python, how to import just words but not any punctuation from a .txt file?
<p>For example ,I have a txt file which content is:</p>

<blockquote>
  <p>Blockquote</p>
</blockquote>

<pre><code>star, year, op, ed
ad, ed, offer, year
</code></pre>

<blockquote>
  <p>Blockquote</p>
</blockquote>

<p>I want to import them and form a list which have each line as a sublist:
    [['star','year','op','ed'],['ad','ed','offer','year']]
So I use the command below:</p>

<blockquote>
  <p>Blockquote</p>
</blockquote>

<pre><code>list = []
with open ("file_name", 'r') as f:
    for line in f:
        split_line = line.split()
        list.append(split_line)
f.close()
</code></pre>

<p>But when I print the list, the result is:</p>

<blockquote>
  <p>Blockquote</p>
</blockquote>

<pre><code>[['star,','year,','op,','ed'],['ad,','ed,','offer,','year']]
</code></pre>

<p>So how can I get a list with only words but not any punctuations?</p>

<p>All you need is splitting with comma and space:</p>

<pre><code>with open ("file_name") as f:
    result = [line.split(', ') for line in f]
</code></pre>

<p>And note that you don't need to close the file manually when you are using <code>whith</code> statement. That's exactly what the <code>with</code> does at the end of the block. And another note, don't name your variable names with python built-in names.</p>

<p>As another alternative (and more pythonic approach) for this task you can use <code>csv</code> module which will split your lines automatically with a delimiter (by default the comma).</p>

<pre><code>import csv
with open ("file_name") as f:
    spam_reader = csv.reader(f) # you can pass the delimiter to reader function (if its something else rather than comma)
    rows = list(spam_reader) 
</code></pre>

python python-3.x split