Zeno
Zeno

Reputation: 3

Splitting a line with different separators in python

I have a text file containing some data stored in three columns. The first column is separated from the second by a comma (,) and a tab (\t) and the same for the separation between the second and the third. However, the last column is terminated with a comma and a newline command (\n). Here is an example:

0.782470450031, 0.0,    0.0,
0.775811285325, 0.025,  0.0,
0.768594334758, 0.05,   0.0,
0.761101295788, 0.075,  0.0,

I would like to read this file and transform it into an array. If the columns were only separated by a comma I would just do:

f=open(filename,'r')
data=[map(float,line.split(',')) for line in f]
data=np.array(data)

But I am not exactly sure how to do this in this case.

Thanks in advance for your help!

Upvotes: 0

Views: 95

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

I did use re module.

data=[map(float,re.split(r',\t*', line)) for line in f]
data=np.array(data)

Upvotes: 0

Maroun
Maroun

Reputation: 95968

Use a regex:

re.split(r',\t', line)

If this is CSV file, you don't need regex, there are many tools that does that for you.

Upvotes: 1

Related Questions