Pk19000 Pk19000 - 2 months ago 5
Python Question

split in python return an excess blank character

I have a file with some data that I read,split with

space
,
,
,
\n
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:

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


input:

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


output (the fifth column is excess) :

[['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', '']]

Answer

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 str.rstrip):

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

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

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