Issue
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
Solution
Your dtype
isn't fine. It's specifying '<f8'
, a float, for each of the fields. You want 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 have removed extraneous stuff about delimiters within quotes)
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.