gpt4 book ai didi

python - genfromtxt 返回 NaN 行

转载 作者:太空狗 更新时间:2023-10-30 00:31:57 25 4
gpt4 key购买 nike

我正在尝试用 numpy 读取一个 csv 文件,我有以下代码

from 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=[('name', '<f8'), ('severity', '<f8'), ('Message', '<f8'), ('AppDomainName', '<f8'), ('ProcessName', '<f8'), ('clientid', '<f8'), ('type', '<f8')])

dtype 看起来不错

为了证明我没有发疯,我尝试了这段代码

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

效果很好,但我想弄清楚 genfromtxt 是怎么回事

这是来自 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

最佳答案

你的 dtype不好。它指定 '<f8' ,一个 float ,用于每个字段。你想要字符串。尝试 dtype=None :

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

产生:

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')])

(我删除了引号内有关定界符的无关内容)

关于python - genfromtxt 返回 NaN 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com