Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

genfromtxt returning NaN rows

Tags:

python

csv

numpy

I am trying to read a csv file with numpy and I have the following code

from numpy import genfromtxt
data = genfromtxt(open('errerr.csv', "r"), names=True, delimiter=',')

and the following comes out

  (nan, nan, nan, nan, nan, nan, nan),
       (nan, nan, nan, nan, nan, nan, nan),
       (nan, nan, nan, nan, nan, nan, nan)], 
      dtype=[('name', '<f8'), ('severity', '<f8'), ('Message', '<f8'), ('AppDomainName', '<f8'), ('ProcessName', '<f8'), ('clientid', '<f8'), ('type', '<f8')])

dtype looks fine

and just to prove I'm not going crazy I tried this code

import csv
f = open('errors.csv', 'rt')
reader = csv.reader(f)
data = [] 
for r in reader: 
    data.append(r)
f.close()

which works great, but im trying to figure out whats the deal with genfromtxt

here is a sample from the csv

name,severity,Message,AppDomainName,ProcessName,clientid,type
 Strings strings,Error,")  Thread Name:  Extended Properties:",SunDSrvc.exe,C:\Program Files\\SunDSrvc.exe,5DAA9377 ,Client
 Strings strings,Error,")  Thread Name:  Extended Properties:",SunDSrvc.exe,C:\Program Files\\SunDSrvc.exe,5DAA9377 ,Client
 Strings strings,Error,")  Thread Name:  Extended Properties:",SunDSrvc.exe,C:\Program Files\\SunDSrvc.exe,5DAA9377 ,Client
like image 660
Marcom Avatar asked Nov 21 '14 16:11

Marcom


1 Answers

You should also add encoding=None to avoid having the Deprecated Warning:

VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default.

Your line should be like:

np.genfromtxt(txt, delimiter=',', names=True, dtype=None, encoding=None)
like image 71
ArgiesDario Avatar answered Oct 01 '22 04:10

ArgiesDario