0

I want to read a tab separated file into a 2-D array where each line get stored in my 2-D array. I tried open and readline but nothing is working correct for me. Lets say my txt file is something like this :

1 2 3 4
2 3 4 5 
3 4 5 6 
...

so what I want is my 2-D array should store array[0]=[1,2,3,4], array[1]=[2,3,4,5] and so on.

2
  • how do you know to when to split the text file into a new array? Commented Aug 27, 2015 at 14:49
  • 1
    Please complete your question with code so that someone can understand better what you need... Commented Aug 27, 2015 at 14:50

2 Answers 2

1

From here

import csv
list(csv.reader(open('text.txt', 'rb'), delimiter='\t'))
Sign up to request clarification or add additional context in comments.

Comments

0

If it is numerical data I suggest the following: use numpy like this

import numpy as np 

data = np.loadtxt('data.dat')

Optionally you can also specify a data type:

import numpy as np 

data = np.loadtxt('data.dat', dtype=np.float64)

If it is not numerical data, I would recommend csv like shown in the answer of CasualDemon:

import csv
list(csv.reader(open('data.dat', 'rb'), delimiter='\t'))

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.