2

I have a .dat file generated from MATLAB (with float values). I need to convert it to an array containing the same number of rows and columns as it did in the .dat file. Could anyone suggest a way of doing this? There is a \n at the end of each line which I want to remove. The .dat file read into Python looks like this:

12,18\n42,17\n60,16\n86,17\n120,17\n137,12\n169,10\n189,14\n215,9\n248,11\n273,12\n301,17\n319,8\n345,17\n378,14\n403,10\n423,10\n453,18\n483,10\n507,15\n10,43\n43,44\n60,38\n96,41\n114,41\n139,41\n168,43\n189,40\n215,37\n248,45\n268,45\n301,37\n321,35\n

And I would like the output to be in the following format:

12,18
42,17
...

where 12,42 and 18,17 are in different columns.

4
  • Can you give some sample data? Will the number of columns differ for each row? Commented Jun 28, 2012 at 14:45
  • Presumably this is a follow up question to your previous question. If you found answers to this previous question helpful then don't forget to upvote those helpful answers and accept one of them. Commented Jun 28, 2012 at 15:11
  • @Chris: Care to explain why you changed 12,18 to 12, 18 in the output format description ? That's not the same thing. Commented Jun 28, 2012 at 15:17
  • 1
    @lqc It was an assumption on my part that the data was made up of two columns of data. I noticed that your comment suggests , is a decimal place (it's not here in the UK) so I have rolled back the changes. @user1482980 can you clarify this point at all? Commented Jun 28, 2012 at 15:19

1 Answer 1

1

what do you mean by array?

with open('datafile','r') as f:
    rows=[map(float,L.strip().split(',')) for L in f]  # list of lists

arr=np.array(rows)  #Numpy array (assuming you `import numpy as np`)

Or using numpy.loadtxt:

arr=np.loadtxt('datafilename',delimiter=',')
Sign up to request clarification or add additional context in comments.

7 Comments

@devsundar: I've edited the post and given the sample data and the output format.
:i have an additional \n character due to which hte above code u suggested gives an error(ValueError: invalid literal for float(): 12,18)
call loadtxt() if you use numpy
Downvoter : Care to elaborate (Or at least post an alternative so the user isn't left wondering what (s)he should do since this is apparently a bad way to do it)?
@lqc -- I'm aware of that (although I don't think about it often). Anyway, in this case, it's obvious that the ',' is being used as a field separator. He chose to store the data in a csv file, not me ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.