None类型对象不可下标化 -- 使用 `np.fromregex`

2024-03-29 09:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题有很多答案(见Python Math - TypeError: 'NoneType' object is not subscriptable)。我的问题不同,因为我正确地期望np.genfromtxt(...)返回一个数组(即np.genfromtxt(...)不是一个就地函数)。在

我正在尝试将以下内容解析并存储到一维数组中:

http://pastie.org/10860707#2-3

为此,我尝试:

pattern = re.compile(b'[\s,]')
theta = np.fromregex("RegLogTheta", regexp = pattern, dtype = float)

这是回溯(应该如何格式化?)公司名称:

^{pr2}$

如果要运行此程序,请从:http://pastie.org/10860707#2-3下载文本文件并运行上面的代码。在


Tags: 答案orghttpobjectisnpnotmath
1条回答
网友
1楼 · 发布于 2024-03-29 09:41:43

文件有多行,用逗号分隔,每行3个数字,除了最后一行只有2个数字

In [182]: fname='../Downloads/pastie-10860707.txt'

In [183]: np.fromregex(fname,regexp=pattern,dtype=float)
... 
np.fromregex(fname,regexp=pattern,dtype=float)

/usr/lib/python3/dist-packages/numpy/lib/npyio.py in fromregex(file, regexp, dtype)
   1240             # Create the new array as a single data-type and then
   1241             #   re-interpret as a single-field structured array.
-> 1242             newdtype = np.dtype(dtype[dtype.names[0]])
   1243             output = np.array(seq, dtype=newdtype)
   1244             output.dtype = dtype

TypeError: 'NoneType' object is not subscriptable

加载了一个简单的“br”read,文件看起来像:

^{pr2}$

最后一行缺少的数字将产生genfromtxt问题。在

你选择的图案是错误的。它看起来像一个分隔符模式。但是fromregex文档中的模式生成组:

regexp = r"(\\d+)\\s+(...)"

fromregex可以

seq = regexp.findall(file.read())  # read whole file and group it
output = np.array(seq, dtype=dtype)  # make array from seq

如果你想使用fromregex,你需要想出一个模式来产生一个元组列表,这些元组可以直接转换成数组。在

==============

尽管再次查看错误消息,我发现直接的问题是dtypedtype=float不是此函数的有效数据类型规范。它需要一个复合数据类型(结构化)。在

此操作会产生错误,其中float是您的dtype参数:

In [189]: np.dtype(float).names[0]
 ...
TypeError: 'NoneType' object is not subscriptable

但它试图这么做是因为模式已经产生了

In [194]: pattern.findall(txt)
Out[194]: 
[b',',
 b',',
 b',',
 b'\n',
 b',',
 b' ',
 b' ',
 ....]

不是它期望的元组列表。在

=================

我可以用

In [213]: np.genfromtxt(txt.splitlines(),delimiter=',',usecols=[0,1])
Out[213]: 
array([[  2.75386225e+00,   1.80508078e+00],
       [ -4.21413726e+00,  -3.38139076e+00],
       [  7.46991792e-01,  -1.08010066e+00],
        ...
       [  4.23010784e-01,  -1.14839331e+00],
       [ -1.15019836e+00,   1.13845303e-06]])

我使用usecols暂时解决最后一行只有2个数字的问题。在

如果删除\n并将其拆分为逗号,则可以直接使用np.array解析结果文本字段。在

In [231]: txt1=txt.replace(b'\n',b'').split(b',')

In [232]: np.array(txt1,float)
Out[232]: 
array([  2.75386225e+00,   1.80508078e+00,   2.95729122e+00,
        -4.21413726e+00,  -3.38139076e+00,  -4.22751379e+00,
          ...
         4.23010784e-01,  -1.14839331e+00,  -9.56098910e-01,
        -1.15019836e+00,   1.13845303e-06])

此模式包括十进制和科学记数法:

In [266]: pattern=re.compile(br"(\d+\.\d+e[\+\-]\d+)")

In [267]: np.fromregex(fname,regexp=pattern,dtype=np.dtype([('f0',float)]))['f0']
Out[267]: 
array([  2.75386225e+00,   1.80508078e+00,   2.95729122e+00,
         4.21413726e+00,   3.38139076e+00,   4.22751379e+00,
      ...
         4.23010784e-01,   1.14839331e+00,   9.56098910e-01,
         1.15019836e+00,   1.13845303e-06])

现在我正在创建一个结构化数组并提取该字段。也许有办法解决这个问题。但是fromregex似乎倾向于使用结构化数据类型。在

相关问题 更多 >