2

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

2 Answers 2

2

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.

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

Comments

2

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

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.