python - “numpy.ndarray”对象不可调用文件未正确选择

标签 python arrays file numpy simplejson

我有以下代码。

fw = open(os.path.join(result_folder, 'motor_commands.txt'),'w')

np.set_printoptions(precision=3)

for positioner in positioner_grid.positioners[1:]:
    simplejson.dump(('Positioner ' + str(positioner.ident)), fw)
    fw.write('\n')
    array = np.array(positioner.motor1.position_array) * 180/math.pi
    array = np.round(array, decimals=4)
    simplejson.dump(array.tolist(), fw)
    fw.write('\n')
    array = np.array(positioner.motor2.position_array) * 180/math.pi
    array = np.round(array, decimals=4)
    simplejson.dump(array.tolist(), fw)
    fw.write('\n \n')
fw.close()

它的输出是一个motor_commands.txt,其布局(例如,对于2个定位器)如下所示(以下符号条目实际上是数字):

"Positioner 1"
[P11, P12, P13]
[Q11, Q12, Q13]

"Positioner 2"
[P21, P22, P23]
[Q21, Q22, Q23]

给定一个预定义的变量dt,我需要生成另一个名为trajectories.yaml的文件,其格式如下:

1:
    alpha: [[P11,0*dt],[P12,1*dt],[P13,2*dt]]
    beta:  [[Q11,0*dt],[Q12,1*dt],[Q13,2*dt]]
2:
    alpha: [[P21,0*dt],[P22,1*dt],[P23,2*dt]]
    beta:  [[Q21,0*dt],[Q22,1*dt],[Q23,2*dt]]

所以,这是我迄今为止尝试生成上面布局的方法。

    tw = open(os.path.join(result_folder, 'trajectories.yaml'), 'w')

    for positioner in positioner_grid.positioners[1:]:
        simplejson.dump((str(positioner.ident) + ':'), tw)
        tw.write('\n')
        simplejson.dump(('alpha: '), tw)
        position1 = []
        for i in range(1, len(positioner.motor1.position_array)):
            temp = [positioner.motor1.position_array[i] * 180 / math.pi, (i-1) * dt]
            position1.append(temp)
        simplejson.dump(array[position1].tolist(), tw)
        tw.write('\n')
        simplejson.dump(('beta: '), tw)
        position2 = []
        for i in range(1, len(positioner.motor2.position_array)):
            temp = [positioner.motor2.position_array[i] * 180 / math.pi, (i-1) * dt]
            position2.append(temp)
        simplejson.dump(array[position2].tolist(), tw)
        tw.write('\n')
    tw.close()

但是抛出以下错误:

'numpy.ndarray' object is not callable Files are not properly selected.

并且我的 trajectories.yaml 文件未正确生成:

"1:"
"alpha: "

请问我做错了什么吗?

最佳答案

很难确定,但我相信您的问题将通过以下方式得到解决。

tw = open(os.path.join(result_folder, 'trajectories.yaml'), 'w')

for positioner in positioner_grid.positioners[1:]:
    tw.write(str(positioner.ident) + ':')
    tw.write('\n')
    tw.write('    alpha: ')
    position1 = []
    for i in range(0, len(positioner.motor1.position_array)):
        temp = [positioner.motor1.position_array[i] * 180 / math.pi, i * dt]
        position1.append(temp)
    simplejson.dump(position1, tw)
    tw.write('\n')
    tw.write('    beta: ')
    position2 = []
    for i in range(0, len(positioner.motor2.position_array)):
        temp = [positioner.motor2.position_array[i] * 180 / math.pi, i * dt]
        position2.append(temp)
    simplejson.dump(position2, tw)
    tw.write('\n')
tw.close()

如果我们使用math. Degrees()enumerate()和列表推导式,这段代码可以大大缩短。此外,open() 的首选习惯用法是 with open(...):,它保证文件将被关闭。

考虑到这一点,上面的内容可以重写为:

import math

with open(os.path.join(result_folder, 'trajectories.yaml'), 'w') as tw:
    for p in positioner_grid.positioners[1:]:
        tw.write(str(p.ident) + ':\n    alpha: ')
        position1 = [[math.degrees(pos), i * dt] for i, pos in enumerate(p.motor1.position_array)]
        simplejson.dump(position1, tw)
        tw.write('\n    beta: ')
        position2 = [[math.degrees(pos), i * dt] for i, pos in enumerate(p.motor2.position_array)]
        simplejson.dump(position2, tw)
        tw.write('\n')

关于python - “numpy.ndarray”对象不可调用文件未正确选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53865389/

相关文章:

python - conda 环境可以继承基础包吗?

c++ - C++中比较不相等数组或字符串的方法

java - JAVA中在文件的一行末尾添加特定字符串

python - Numpy 将函数应用于选定的矩阵索引

iphone - Objective c 将字符串对象添加到数组

file - 在类里面保存文件或 channel 的好习惯

android - 列出Android手机下载目录下的文件

python - Pandas : Pivot row into column

python - 我如何在选择排序中计算这些比较?

Python:使用 locals() 进行编程类实例变量初始化