0

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!

Zeno
  • 3
  • 1

2 Answers2

1

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

I did use re module.

data=[map(float,re.split(r',\t*', line)) for line in f]
data=np.array(data)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274