0

I know this is probably pretty basic, but I keep getting following error when I try to load my file into python.like

ValueError: could not convert string to float

I have tried

input = np.loadtxt("input.txt", dtype='i', delimiter=' ')
open(file.txt,'r')

and read csv but can't get it to read properly, probably just some small mistake.

The file look like the example below. I don't need the top row, as they just explain limits and the step size between the data. there are multiple spaces between the numbers and some empty lines

Thanks

-89.9916666667 89.9916666667   -0.0083333333  360.0083333333    0.0166666667    0.0166666667

 15.0900     15.0740     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0740     15.0740     15.0740     15.0740
 15.0740     15.0740     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0720     15.0720     15.0720
 15.0720     15.0720     15.0720     15.0720     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0720
 15.0720     15.0730     15.0730     15.0740     15.0740     15.0740
 15.0740     15.0740     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0740     15.0740     15.0750     15.0750
 15.0760     15.0760     15.0760     15.0760     15.0760     15.0750
 15.0750     15.0740     15.0740     15.0740     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0730     15.0730     15.0730
 15.0740     15.0740     15.0740     15.0750     15.0750     15.0750
 15.0760     15.0760     15.0760     15.0760     15.0760     15.0760
 15.0760     15.0760     15.0760     15.0760     15.0750     15.0740
 15.0740     15.0740     15.0740     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0720     15.0720     15.0720     15.0720
 15.0720     15.0720     15.0720     15.0720     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0710
 15.0710     15.0710     15.0720     15.0720     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0740     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0730     15.0730     15.0730     15.0730     15.0720
 15.0720     15.0720     15.0710     15.0710     15.0700     15.0700
 15.0700     15.0710     15.0710     15.0710     15.0720     15.0720
 15.0720     15.0730     15.0730     15.0730     15.0740     15.0740

 15.0740     15.0740     15.0740     15.0740     15.0740     15.0740
 15.0740     15.0730     15.0730     15.0730     15.0730     15.0730
 15.0730     15.0720     15.0720     15.0720     15.0720     15.0720
 15.0720     15.0710     15.0710     15.0710     15.0710     15.0710
 15.0710     15.0710     15.0710     15.0710     15.0710     15.0710
  • `"ValueError: could not convert string to float: "` occurs while trying to set a variable without casting it or parsing properly. If you cast the string to float with just `float(input_float)` it should work out properly. Ruling out the parsing errors of course otherwise check @FlorenceWilliams comment – Zeromika Mar 16 '19 at 10:26
  • Please check below url. I think this question is duplicated https://stackoverflow.com/questions/8420143 –  Mar 16 '19 at 10:26
  • 1
    You could copy+paste small section of your file into the question. It would be more helpful instead of inserting a picture. Is there an empty line in your file? `open(file.txt,'r')` is incorrect, probable you wanted: `open('file.txt','r')` – quantummind Mar 16 '19 at 10:46
  • @FlorenceWilliams with that approach i can append only the first row and then get the error ´ValueError: not enough values to unpack (expected 6, got 0)´ – Mathias Lindeberg Kristensen Mar 16 '19 at 11:11

1 Answers1

1

It seems like you have two empty lines in your input.txt file, as well as inconsistent spacing between your values. The empty lines need to be removed.

To illustrate, in the first line you have:

-89.9916666667 89.9916666667   -0.0083333333  360.0083333333    0.0166666667    0.0166666667

The first values are separated with one whitespace, and the others have 3, 2 and 4, respectively. When you specify a delimiter in your call to np.loadtxt("input.txt", dtype='i', delimiter=' '), you're specifying a single whitespace character. Simply remove the delimiter keyword argument to accept any number of whitespace characters as a delimiter.

Furthermore, your call also specifies the input data type as an integer, with dtype='i'. I don't know if this is intentional, but this will round all values to the nearest integer. Looking at your data, this is probably not what you want.

Lastly, your call to open('input.txt', 'r') does nothing, as np.readtxt handles opening and closing of files by itself.

Hopefully this solves your problem.

polterzeit
  • 56
  • 3
  • Hmm that seems about right. It seems though that i have only 2 colums on the last row. Is there any way to still load the data, and just have nan or something in the end without editing the text file.? – Mathias Lindeberg Kristensen Mar 16 '19 at 11:42
  • If your input data is lacking columns, then you shouldn't use numpy to read the data, as it will assume your file is correctly formatted. In this case, you should probably `open('input.txt', 'r') as infile:` and read the data into an array in a loop. – polterzeit Mar 16 '19 at 11:56
  • Alternatively, read the data into a pandas data frame :) – polterzeit Mar 16 '19 at 11:59