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!

3
  • stackoverflow.com/questions/4315506/… Commented Jul 16, 2015 at 15:05
  • Use the csv module. Commented Jul 16, 2015 at 15:07
  • Don't even worry about the extra white space, tabs, etc. The float conversion will discard all of that for example float('\t12.5\t\n') will produce the float 12.5. Commented Jul 16, 2015 at 15:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I did use re module.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.