split in python return an excess blank character
<p>I have a file with some data that I read,split with <code>space</code>,<code>,</code> ,<code>\n</code> and take it in a matrix.
But my code return an excess blank character into my matrix. Can anybody help me find this bug? Thanks.
code:</p>

<pre><code>import re
lines = [re.split('[,\n ]',line) for line in open('lines.txt')]
print lines
</code></pre>

<p>input:</p>

<pre><code>395,0 398,100
398,100 488,196
488,196 544,233
544,233 506,301
506,301 425,344
425,344 336,355
336,355 271,319
271,319 293,264
293,264 328,232
328,232 329,170
329,170 267,175
267,175 228,199
228,199 214,220
214,220 80,268
80,268 0,273
0,183 96,176
96,176 168,92
168,92 252,124
252,124 300,88
300,88 303,40
303,40 309,0
</code></pre>

<p>output (the fifth column is excess) :</p>

<pre><code>[['395', '0', '398', '100', ''], ['398', '100', '488', '196', ''], ['488', '196', '544', '233', ''], ['544', '233', '506', '301', ''], ['506', '301', '425', '344', ''], ['425', '344', '336', '355', ''], ['336', '355', '271', '319', ''], ['271', '319', '293', '264', ''], ['293', '264', '328', '232', ''], ['328', '232', '329', '170', ''], ['329', '170', '267', '175', ''], ['267', '175', '228', '199', ''], ['228', '199', '214', '220', ''], ['214', '220', '80', '268', ''], ['80', '268', '0', '273', ''], ['0', '183', '96', '176', ''], ['96', '176', '168', '92', ''], ['168', '92', '252', '124', ''], ['252', '124', '300', '88', ''], ['300', '88', '303', '40', ''], ['303', '40', '309', '0', '']]
</code></pre>

<p>lines read from a text file generally have a newline on the end (unless they're the last line in which case they might not).  It's pretty common to see that newline stripped off (e.g. using <a href="https://docs.python.org/3/library/stdtypes.html#str.rstrip" rel="nofollow"><code>str.rstrip</code></a>):</p>

<pre><code>import re
lines = [re.split('[,\n ]', line.rstrip('\n')) for line in open('lines.txt')]
print lines
</code></pre>

<hr>

<p>As an aside, it's better practice to use a context manager for managing open files:</p>

<pre><code>with open('lines.txt') as input_file:
    lines = [re.split('[,\n ]', line.rstrip('\n')) for line in input_file]
print lines
</code></pre>

python regex split