-1

So this might be a bit of a noob question as I don't have a whole lot of python experience. I have a .dat file that I've converted into .csv that I'm trying to read into python. This should be very simple as numpy has a built in function for this. My code is:

import numpy as np
import csv

d = np.loadtxt('scl1.csv', delimiter="\t")

The error I get says that it could not convert string to float. How do I fix this? For reference the data file has 6 columns, where each entry is a number (ie 7.33390715197523163E-002) and they are delimited by tabs. I thought that the problem could be with the E in some of the numbers but I checked and numpy can read that as a float. Thanks very much for any help.

Edited to include the exact error message if its helpful:

Traceback (most recent call last):
  File "/Applications/Python 3.4/Markov Chain/Monte Carlo.py", line 10, in <module>
    d = np.loadtxt('scl2.dat', delimiter="\t")
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/npyio.py", line 848, in loadtxt
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/npyio.py", line 848, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: could not convert string to float: b'   7.33390715197523163E-002  7.68126324487871659E-002  5.46056179421958582E-002  -22.791933511352461        8.7787362443778942       -3.6715272730208461     '
9
  • 2
    Can you post the first 5 or 6 lines of your file including the header?
    – GWW
    Commented Jul 30, 2014 at 1:51
  • Adding to @GWW's suggestion: make sure the sample demonstrates the error when you try to read it with loadtxt. Commented Jul 30, 2014 at 1:53
  • 2
    Try np.genfromtxt('scl1.csv', delimiter="\t"). With loadtext, make sure your delimiter is correct. Commented Jul 30, 2014 at 2:06
  • here are the first 3 lines of data, there is no header. Not sure if the format of the data will display correctly in the commment though Commented Jul 30, 2014 at 9:17
  • 7.33390715197523163E-002 7.68126324487871659E-002 5.46056179421958582E-002 -22.791933511352461 8.7787362443778942 -3.6715272730208461 -0.11032366115656110 9.61058036106280933E-002 -0.10785545602298079 0.50072238518266166 1.1887691228212711 0.44552227056989957 9.03539417380724713E-002 9.67449743610417495E-002 0.19713039272901062 4.4966587161804403 -3.1198608673245660 -7.7887113623899618 Commented Jul 30, 2014 at 9:18

1 Answer 1

1

loadtxt() assumes the file consists only of floats, but your .csv file (likely) has column headings that are strings--hence the conversion error.

If your csv file contains only numbers and no quotes, ignore the column names by skipping the first row of the file.

np.loadtxt('scl1.csv', delimiter='\t', skiprows=1)

Or to grab the headers as well:

DELIM = '\t'
fh = open('scl1.csv')
headers = fh.readline().strip().split(DELIM)
data = np.loadtxt(fh, delimiter=DELIM)

Otherwise, you could use the csv module and cast the types yourself.

read_csv = csv.reader(open('scl1.csv'))
for fields in read_csv:
     pass # process fields list, which are all strings, and includes the col names

Also, random tip: don't name things with both an 'l' and a '1' if you can help it!

2
  • The data file has no headers, is only numbers and the occasional E signifying an exponent. I need to read the data as floats because I will be using numpy to carry out calculations on the first column. will the csv module you posted work for that? thanks Commented Jul 30, 2014 at 9:21
  • Like I said in the answer: csv will return strings. I think you should try the code for yourself and see if you can get it to work BEFORE asking "if it will work." That's how we learn.
    – sp00n3r
    Commented Jul 30, 2014 at 23:46

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.