0

I'm traing to convert lines of data into a Numpy array.

This is the first line:

['2013-08-14 00:00:00', '232598', '1.3999999761581', '', '1.1572500056095', '12.302269935608', '51.526794433594', '2.2079374790192', '0.60759371519089', '23.152534484863', '']

Then

import numpy as np
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
 values = [ float(x) for x in line.split(',')[1:]]
 float_data[i, :] = values

I'm getting: ValueError: could not convert string to float: I guess a string whit the value ' ' is the cause of the problem. How can I replace the ' ' with the value 0?

2 Answers 2

1

You could use:

values = [float(x) if len(x) else 0.0 for x in line.split(',')[1:]]

If you are reading a example.txt file like this:

v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10
2013-08-1400:00:00,232598,1.3999999761581,,1.1572500056095,12.302269935608,51.526794433594,2.2079374790192,0.60759371519089,23.152534484863,
2013-08-1400:00:00,232598,1.3999999761581,,1.1572500056095,12.302269935608,51.526794433594,2.2079374790192,0.60759371519089,23.152534484863,

This code reads the data into float values and stores them in the float_data array.

file = open("example.txt", "r")
lines = [line.rstrip('\n') for line in file]
header = lines[0].split(',')
lines = lines[1:]
float_data = np.zeros((len(lines), len(header) - 1))

for i, line in enumerate(lines):
    values = [float(x) if len(x) else 0.0 for x in line.split(',')[1:]]
    float_data[i, :] = values
Sign up to request clarification or add additional context in comments.

2 Comments

Whitthis i'm getting: "ValueError: cannot copy sequence with size 0 to array axis with dimension 10"
I couldn't reproduce your last error. Are you sure all of the lines are of the same length?
0

Finally, this worked out for me

import numpy as np
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):   
  laLinea = line.split(',')
  for n, k in enumerate(laLinea):
    if k == '':
      laLinea[n] = '0.0'
  laLinea = laLinea[1:]      
  for x in laLinea:    
    values = [float(x)  for x in laLinea] 
    float_data[i, :] = values

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.