本文介绍了genfromtxt返回NaN行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用numpy读取csv文件,我有以下代码
来自numpy import genfromtxt
data = genfromtxt(open('errerr.csv',r),names = True,delimiter =',')
并出现以下结果
(nan,nan,nan,nan,nan,nan,nan ,
(nan,nan,nan,nan,nan,nan,nan),
(nan,nan,nan,nan,nan,nan,nan)],
dtype = ('App','App','''),('name','< f8'),('severity','< f8' ProcessName','< f8'),('clientid','< f8'),('type','< f8')])
dtype看起来不错
只是为了证明我不会疯狂我试过这个代码
import csv
pre>
f = open('errors.csv','rt')
reader = csv.reader )
data = []
for r in reader:
data.append(r)
f.close()
这很好,但我想知道什么是与genfromtxt交易
来自csv
name,severity,Message,AppDomainName,ProcessName,clientid,type
字符串, )线程名:扩展属性:,SunDSrvc.exe,C:\Program Files\\SunDSrvc.exe,5DAA9377,Client
字符串字符串,错误,)线程名:扩展属性:,SunDSrvc .exe,C:\Program Files\\SunDSrvc.exe,5DAA9377,Client
字符串,错误,)线程名:扩展属性:,SunDSrvc.exe,C:\Program Files \\ \\\SunDSrvc.exe,5DAA9377,客户
解决方案
dtype
不正常。它为每个字段指定'< f8'
,一个浮点数。你想要的字符串。尝试dtype = None
:np.genfromtxt(txt,delimiter = ',',names = True,dtype = None)
$ b数组(['Strings strings','Error',')线程名:扩展属性:','SunDSrvc.exe' C:\\Program Files\\SunDSrvc.exe','5DAA9377','Client'),
('Strings strings','Error',' ','SunDSrvc.exe','C:\\Program Files\\SunDSrvc.exe','5DAA9377','Client'),
('Strings strings','Error' )线程名称:扩展属性:','SunDSrvc.exe','C:\\Program Files\\SunDSrvc.exe','5DAA9377','Client')],
dtype ='('Name','S15'),('severity','S5'),('Message','S39'),'AppDomainName','S12' ),('clientid','S9'),('type','S6')])
(我已经删除了关于引号内的分隔符的无关的东西)
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
解决方案Your
dtype
isn't fine. It's specifying'<f8'
, a float, for each of the fields. You want strings. Trydtype=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)
这篇关于genfromtxt返回NaN行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!