0

I want to use numpy's loadtxt command, so I tried this:

lines = loadtxt("D:/My Desktop/Enwate syllables.txt", comments="#", delimiter=",", unpack=False)

but that fails hard with "could not convert string to float: b'\xef\xbb\xbfen'"

I did try

f = open("D:/My Desktop/Enwate syllables.txt")

and that works fine. What am I doing wrong with loadtxt?

PS. It's not an encoding issue since as shown above the "open" command works on it perfectly well, I get the text lines and everything. And by the way, the file is a simple ascii text file with one word on each line.

Sindyr
  • 1,277
  • 1
  • 11
  • 16
  • Possible duplicate of [Numpy loadtxt encoding](https://stackoverflow.com/questions/24694736/numpy-loadtxt-encoding) – eyllanesc Oct 31 '17 at 16:10
  • It would help if you gave us a sample of this file. Is '\xef\xbb\xbfen' actually one of those simple ascii words? – hpaulj Oct 31 '17 at 18:34
  • At this point, perhaps I should delete this question, as I stopped using loadtxt and went back to using a simple open, which did what I needed it to do. – Sindyr Oct 31 '17 at 20:34
  • the issue is what I mentioned in the answer ,did it not help – Bg1850 Nov 01 '17 at 01:07
  • I am going to assume it does fix the issue, since I wound up going a different route - thanks though. – Sindyr Nov 02 '17 at 01:45

1 Answers1

0

You need to mention the dtype before hand ,so that numpy can allocate it for example

from io import StringIO 
j= StringIO(u'c 1\nd 2')
np.loadtxt(j)

fails with ValueError: could not convert string to float: c

but

np.loadtxt(j,dtype={'names': ('char', 'num'),'formats': ('S1', 'i4')})

works perfectly fine .

so if your text file has both of numeric data type and character data type ,you need to mention its format using the dtype

P.S -if you try a file with only numeric values loadtxt will not fail ,so I guess its primary data type is float ,if your file have strings ,then you need to use dtype as per your fields type

Bg1850
  • 3,032
  • 2
  • 16
  • 30