2016-09-21 85 views
0

我想在这个文件中读取(的test.txt)NumPy的genfromxt类型错误:数据类型不被理解错误

01.06.2015;00:00:00;0.000;0;-9.999;0;8;0.00;18951;(SPECTRUM)ZERO(/SPECTRUM) 
01.06.2015;00:01:00;0.000;0;-9.999;0;8;0.00;18954;(SPECTRUM)ZERO(/SPECTRUM) 
01.06.2015;00:02:00;0.000;0;-9.999;0;8;0.00;18960;(SPECTRUM)ZERO(/SPECTRUM) 
01.06.2015;09:23:00;0.327;61;25.831;39;29;0.18;19006;01.06.2015;09:23:00;0.327;61;25.831;39;29;0.18;19006;(SPECTRUM);;;;;;;;;;;;;;1;1;;;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;;;;;;;(/SPECTRUM) 
01.06.2015;09:24:00;0.000;0;-9.999;0;29;0.00;19010;(SPECTRUM)ZERO(/SPECTRUM) 

...我与numpy的功能genfromtxt(试过)(请参见下面的代码摘录)。

import numpy as np 
col_names = ["date", "time", "rain_intensity", "weather_code_1", "radar_ref", "weather_code_2", "val6", "rain_accum", "val8", "val9"] 
types = ["object", "object", "float", "uint8", "float", "uint8", "uint8", "float", "uint8","|S10"] 
# Read in the file with np.genfromtxt 
mydata = np.genfromtxt("test.txt", delimiter=";", names=col_names, dtype=types) 

现在,当我执行的代码我碰到下面的错误 - >

raise ValueError(errmsg)ValueError: Some errors were detected ! 
    Line #4 (got 79 columns instead of 10) 

现在我认为,困难来自于最后一列(val9)与许多;;;;;;;
这是很明显,最后一列;中的分隔符和符号是相同的!

如何在文件中读取没有错误的文件,也许有可能跳过最后一列,或仅替换最后一列中的;

+0

标题错误与文本错误不匹配。 – hpaulj

回答

0

usecols可以用来忽略多余的分隔符,例如

In [546]: np.genfromtxt([b'1,2,3',b'1,2,3,,,,,,'], dtype=None, 
    delimiter=',', usecols=np.arange(3)) 
Out[546]: 
array([[1, 2, 3], 
     [1, 2, 3]]) 
+0

是的你是对的,这也适用于上面的例子,很多thxs! – Markus

2

numpy documentation

invalid_raise : bool, optional
If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped.

mydata = np.genfromtxt("test.txt", delimiter=";", names=col_names, dtype=types, invalid_raise = False) 

需要注意的是有你的代码,我已经改正了错误(分隔符拼写错误,并在函数调用被称为dtypestypes列表)

编辑:从您的评论,我看到我有点误解。你的意思是你想跳过最后的而不是最后的

看看下面的代码。我已经定义了一个生成器,它只返回每行的前十个元素。这将允许genfromtxt()完成没有错误,你现在从所有行中获得列#3。

但是请注意,您仍然会丢失一些数据,就好像您仔细观察一样,您会看到问题行实际上是两行连接在一起的垃圾,其他行有ZERO。所以你仍然会失去第二条线。也许你可以修改生成解析每一行,并与此不同的处理,但我会离开,作为一个有趣的练习:)

import numpy as np 

def filegen(filename): 
    with open(filename, 'r') as infile: 
     for line in infile: 
      yield ';'.join(line.split(';')[:10]) 

col_names = ["date", "time", "rain_intensity", "weather_code_1", "radar_ref", "weather_code_2", "val6", "rain_accum", "val8", "val9"] 
dtypes = ["object", "object", "float", "uint8", "float", "uint8", "uint8", "float", "uint8","|S10"] 
# Read in the file with np.genfromtxt 
mydata = np.genfromtxt(filegen('temp.txt'), delimiter=";", names=col_names, dtype = dtypes) 

输出

[('01.06.2015', '00:00:00', 0.0, 0, -9.999, 0, 8, 0.0, 7, '(SPECTRUM)') 
('01.06.2015', '00:01:00', 0.0, 0, -9.999, 0, 8, 0.0, 10, '(SPECTRUM)') 
('01.06.2015', '00:02:00', 0.0, 0, -9.999, 0, 8, 0.0, 16, '(SPECTRUM)') 
('01.06.2015', '09:23:00', 0.327, 61, 25.831, 39, 29, 0.18, 62, '01.06.2015') 
('01.06.2015', '09:24:00', 0.0, 0, -9.999, 0, 29, 0.0, 66, '(SPECTRUM)')] 
+0

好的,谢谢你的帮助和编辑我的错误。 – Markus

+0

@ SiHa,好的,谢谢你的帮助和编辑我的错误。如果我放入(invalid_raise = False)命令并且删除了第4行孔。但我需要这些线(我需要分别在每一行中的第三列)。因为.txt文件更长,并且始终在我感兴趣的第3列中显示。所以,当有;;;;;;;在最后一列的标志,我将分别需要#3列! Thxs – Markus

+0

@Markus查看已更新的答案,但请注意,在应对格式错误的行时,将难以保留*全部*数据。 – SiHa