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]
Azam
  • 165
  • 14
  • 1
    Two problems - the brackets and the comma delimiter. `loadtxt` doesn't handle extraneous characters like [], and use white space as the default delimiter. – hpaulj Dec 26 '17 at 06: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 ;) – kmario23 Dec 26 '17 at 06:07
  • 1
    The quotes also hinder the conversion to floats. `loadtxt` is intended for normal CSV - comma separated files. – hpaulj Dec 26 '17 at 07:32
  • See also [this question](https://stackoverflow.com/questions/16729210/numpy-loadtxt-valueerror-could-not-convert-string-to-float), or of course the documentation. – Tom de Geus Dec 26 '17 at 10:19

0 Answers0