Skip to main content
  1. About
  2. For Teams
Asked
Viewed 486 times
1

I have data in the format of 10000x500 matrix contained in a .txt file. In each row, data points are separated from each other by one whitespace and at the end of each row there a new line starts.

Normally I was able to read this kind of multidimensional array data into Python by using the following snippet of code:

with open("position.txt") as f:
    data = [line.split() for line in f]

# Get the data and convert to floats
ytemp = np.array(data) 
y = ytemp.astype(np.float) 

This code worked until now. When I try to use the exact some code with another set of data formatted in the same way, I get the following error:

setting an array element with a sequence.

When I try to get the 'shape' of ytemp, it gives me the following:

(10001,)

So it converts the rows to array, but not the columns.

I thought of any other information to include, but nothing came to my mind. Basically I'm trying to convert my data from a .txt file to a multidimensional array in Python. The code worked before, but now for some reason that is unclear to me it doesn't work. I tried to look compare the data, of course it's huge, but everything seems quite similar between the data that is working and the data that is not working.

I would be more than happy to provide any other information you may need. Thanks in advance.

1 Answer 1

1

Use numpy's builtin function:

data = numpy.loadtxt('position.txt')

Check out the documentation to explore other available options.

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

1 Comment

Thank you very much, I wouldn't have guessed the solution would be this simple :). I am a newbie in Python and there always seems to be too much syntax for me to pick up.

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.