Reputation: 1114
I want to read the file of below format into numpy array in python.
ADIDGoogle#8a65c466-****-4a7e-****-0836c8884dae 2016-06-01 17:55:53
ADIDGoogle#8a65c466-****-4a7e-****-0836c8884dae 2016-06-01 17:55:53
ADIDGoogle#8a65c466-****-4a7e-****-0836c8884dae 2016-06-01 17:55:53
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:20:02
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:35:48
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:26:20
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:31:12
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:19:17
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:20:02
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:36:39
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:31:12
ADIDGoogle#8a664a70-****-4103-****-4f7e6cb9a33a 2016-06-01 13:35:48
it is having three columns separated by '\t'. i want to read this into numpy array with two columns where the date and time in to one column and id in another column.
I tried using
Data = np.loadtxt(filename,dtype='string',usecols=(1,2),delimiter="\t")
but it is giving me error as :
IndexError: list index out of range
Upvotes: 2
Views: 6989
Reputation: 635
You can read via genfromtxt
line-by-line
import numpy as np
fname = "./data.txt"
with open(fname, 'r') as f:
data = np.genfromtxt(f,comments="!",dtype="string",usecols=(1,2))
print data
Upvotes: 2
Reputation:
First of all, the #
character in your file will make numpy
think everything after "ADIDGoogle" in each line is a comment. It appears you can change the comment character using the comments
kwarg in np.loadtxt
. This will solve the IndexError
, leaving the delimiter problem.
Upvotes: 2