0

I have a text file as follows: enter image description here

Now, I would like to load it into a numpy array or a list of lists which should be in the same order as that of the image above. So, I have to get an array with 3 columns and the number of rows present in the file.

I have used the following code below:

import os
a = open(os.getcwd()+'a.txt', 'r')
a = a.read()

But, I am getting an output as follows which is not a list or an array and is not what I wanted: enter image description here

Can someone please help me out with this?

2
  • 1
    Your code is attempting to read() a directory. It should raise IsADirectoryError Commented Apr 15, 2020 at 4:23
  • Just check it now. I have added the file name! Commented Apr 15, 2020 at 4:24

1 Answer 1

1

This will convert a space-delimited file into a list of lists.

with open('a.txt') as g:
    data = [line.strip().split() for line in g.readlines()]
Sign up to request clarification or add additional context in comments.

Comments

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.