Genfromtxt return strings NaN

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 is issued

  (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 prove that I'm not losing my mind. 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 fine, but I'm trying to figure out what the deal with genfromtxt is

here is an example from 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 
+5
source share
1 answer

Your dtype not suitable. It indicates '<f8' , a float, for each of the fields. You need strings. Try dtype=None :

  np.genfromtxt(txt,delimiter=',',names=True,dtype=None) 

which produces:

 array([ ('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')], dtype=[('name', 'S15'), ('severity', 'S5'), ('Message', 'S39'), ('AppDomainName', 'S12'), ('ProcessName', 'S29'), ('clientid', 'S9'), ('type', 'S6')]) 

(I removed extraneous things about separators inside quotes)

+8
source

Source: https://habr.com/ru/post/1207423/


All Articles