0

I have a text file with two columns of data that are separated by a comma. I am trying to import the data into my python script using numpy loadtext, but I am getting the error: invalid literal for float(): 201.9271,43

All of my data looks like this. How do can I get numpy loadtext to import the data properly?

Here's my code:

import numpy as np 

data = np.loadtxt('Ozone_at_Uva_2001.txt', dtype=object)

dct = data[:,0] #DecTime 

ppbv = data[:,1] #[O3]ppbv

My text file looks like this except for there are many more data points.

201.9271,43

201.9375,35

201.9479,31

201.9583,35

201.9688,31

201.9792,30
2
  • Use delimiter=','. The default delimiter is white space, not a comma. Commented Oct 10, 2017 at 13:27
  • Why are you using dtype='object'? Commented Oct 10, 2017 at 13:28

1 Answer 1

1

Use genfromtxt might work:

import numpy as np
data = np.genfromtxt('Ozone_at_Uva_2001.txt', delimiter=',')
dct = data[:,0]
ppbv = data[:,1]
print dct
print ppbv

Output:

[ 201.9271  201.9375  201.9479  201.9583  201.9688  201.9792]
[ 43.  35.  31.  35.  31.  30.]

If you wanna use loadtxt, generally like this:

data2 = np.loadtxt('Ozone_at_Uva_2001.txt', delimiter=',')
print data2
Sign up to request clarification or add additional context in comments.

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.