使用genfromtxt导入numpy中缺少值的csv数据 [英] Using genfromtxt to import csv data with missing values in numpy
问题描述
我有一个csv文件,看起来像这样(实际文件有更多的列和行):
I have a csv file that looks something like this (actual file has many more columns and rows):
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16
说文件名是 info.csv 如果我尝试使用
Say the name of the file is info.csv If I try to import this using
data = numpy.genfromtxt('info.csv', delimiter = ',')
然后出现以下错误:
ValueError: Some errors were detected ! Line #4 (got 1 columns instead of 5)
如果我使用
data = numpy.genfromtxt('info.csv', delimiter = ',', skip_footer = 1)
数据16
和数据11, 12, 13, 14, 15
的
行均被跳过.我不明白为什么要跳过带有<c1>的行.如果能适当地使用genfromtxt
导入上述文件中的前三行,我将不胜感激.
both lines with data 16
and with data 11, 12, 13, 14, 15
are skipped. I don't understand why the line with 11, 12, 13, 14, 15
is being skipped. I would appreciate any help on how I can appropriately use the genfromtxt
to import first three lines in the above file.
谢谢
推荐答案
如果您可以忽略文件末尾的16,请尝试使用
if you can ignore the 16 at the end of the file try using the
invalid_raise
(布尔型,可选)参数
如果设置为False,它将忽略所有不完整的行而不会引发异常
invalid_raise
(bool, optional) parameter
if set to False it ignores all incomplete lines without throwing an exception
请参见此处(其在示例之前的最后一个参数) http://docs.scipy.org/doc/numpy/reference /generation/numpy.genfromtxt.html
see here (its the last parameter before the examples) http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html
这篇关于使用genfromtxt导入numpy中缺少值的csv数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!