0

I cant load text using numpy.loadtxt for the list of string inside 'file.dat':

['3317121918', '69', '1345', '15']

['3317122000', '72', '1337', '20']

['3317122006', '75', '1330', '20']

['3317122012', '78', '1321', '20']

['3317122018', '83', '1310', '25']

['3317122100', '86', '1295', '35']

['3317122106', '85', '1284', '35']

['3317122112', '84', '1276', '40']

['3317122118', '79', '1267', '50']

Code used now.

import numpy as np
data=np.loadtxt('file.dat')
x=data[:,1]
y=data[:,2]
z=data[:,3]

I am trying to assign those values in proper column from this command but the iteration says "could not convert string to float:". How to convert this list of string to float?.

Expected output for x, y and z are supposed to be:

x=[69,72,75,78,83,85,85,84,70]
y=[1345,1337,1330,1321,1310,1295,1284,1276,1267]
z=[15,20,20,20,25,35,35,40,50]
4
  • 1
    Two problems - the brackets and the comma delimiter. loadtxt doesn't handle extraneous characters like [], and use white space as the default delimiter. Commented Dec 26, 2017 at 6:02
  • @hpaulj well, we can handle that by iterating over lines and making the result in an array format. But, I'm not sure OP is okay with that ;) Commented Dec 26, 2017 at 6:07
  • 1
    The quotes also hinder the conversion to floats. loadtxt is intended for normal CSV - comma separated files. Commented Dec 26, 2017 at 7:32
  • See also this question, or of course the documentation. Commented Dec 26, 2017 at 10:19

0

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.