【问题标题】:Adding names parameter to numpy genfromtxt causing to read empty strings for data将名称参数添加到 numpy genfromtxt 导致读取数据的空字符串
【发布时间】:2023-04-03 16:36:01
【问题描述】:

我可以很好地读取数据文件,但是当我尝试通过自己指定名称或从第一行读取来添加名称参数时,我会返回空字符串

data_no_headers = genfromtxt('SimpleDataWithHeaders.csv',delimiter=',',dtype='str',autostrip=True)
print(data_no_headers)
data_with_headers = genfromtxt('SimpleDataWithHeaders.csv',delimiter=',',dtype='str',autostrip=True,names=True)
print(data_with_headers)
data_with_headers = genfromtxt('SimpleDataWithHeaders.csv',delimiter=',',skip_header=1,dtype='str',autostrip=True,names="A,B")
print(data_with_headers)
mycols = ['a','b']
data_with_headers = genfromtxt('SimpleDataWithHeaders.csv',delimiter=',',skip_header=1,dtype='str',autostrip=True,names=mycols)
print(data_with_headers)

如果我执行此代码,我会得到以下输出(我制作了一个非常简单的 csv 文件,其中包含三行和一个标题行来说明问题),您可以看到上面每个命令的输出。在我添加名称参数之前,您可以看到它工作正常

[['CODE' 'AIRPORT']
['HOU' 'Houston']
['ABQ' 'Alberquerque']
['BWI' 'Baltimore']]

[('', '') ('', '') ('', '')]

[('', '') ('', '') ('', '')]

[('', '') ('', '') ('', '')]

【问题讨论】:

  • 使用names时,还要指定dtype=Nonedtype=str 使字段 dtype 'U',一个 0 元素字符串,因此是 '' 结果)。如果您查看data.dtypeprint(repr(data)),问题会更加明显。
  • 如果你不需要带有字段的结构化数组,你可以跳过标题,只得到一个 (n,2) 字符串 dtype 数组。
  • 更改 dtype=None 并添加 encoding=None 确实会读取读入的值,但现在它不再创建多行,而是得到单行对...
  • data_with_headers = genfromtxt('SimpleDataWithHeaders.csv',delimiter=',',dtype=None, encoding=None,autostrip=True,names=True) print(data_with_headers)
  • [('HOU', 'Houston') ('ABQ', 'Alberquerque') ('BWI', 'Baltimore')]

标签: python numpy genfromtxt


【解决方案1】:

模拟文件:

In [243]: txt = """CODE, AIRPORT 
     ...: HOU, Houston 
     ...: ABQ, Alberquerque 
     ...: BWI, Baltimore"""                                                               

不使用标题阅读:

In [244]: data = np.genfromtxt(txt.splitlines(), delimiter=',', dtype=str, skip_header=1, 
     ...: encoding=True)                                                                  
In [245]: data                                                                            
Out[245]: 
array([['HOU', ' Houston'],
       ['ABQ', ' Alberquerque'],
       ['BWI', ' Baltimore']], dtype='<U13')

结果是具有字符串 dtype 的二维数组。

使用标头和 dtype=None:

In [246]: data = np.genfromtxt(txt.splitlines(), delimiter=',', dtype=None, names=True, en
     ...: coding=True)                                                                    
In [247]: data                                                                            
Out[247]: 
array([('HOU', ' Houston'), ('ABQ', ' Alberquerque'),
       ('BWI', ' Baltimore')],
      dtype=[('CODE', '<U3'), ('AIRPORT', '<U13')])
In [248]: data.shape                                                                      
Out[248]: (3,)
In [249]: data['CODE']                                                                    
Out[249]: array(['HOU', 'ABQ', 'BWI'], dtype='<U3')

结果是一个结构化数组 - 1d 有 2 个字段,按名称访问。

使用str dtype,也是结构化的,但是dtype是'U',一个0字节的字符串,所以显示的是空字符串:

In [250]: data = np.genfromtxt(txt.splitlines(), delimiter=',', dtype=str, names=True, enc
     ...: oding=True)                                                                     
In [251]: data                                                                            
Out[251]: 
array([('', ''), ('', ''), ('', '')],
      dtype={'names':['CODE','AIRPORT'], 'formats':['<U','<U'], 'offsets':[0,0], 'itemsize':2})

Plain print 省略了 dtype,可能会造成混淆:

In [252]: print(data)                                                                     
[('', '') ('', '') ('', '')]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-24
    • 2017-03-02
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多