解决 AttributeError: 'str' object has no attribute 'read'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决 AttributeError: 'str' object has no attribute 'read'相关的知识,希望对你有一定的参考价值。

参考技术A

使用Python的 flask 框架写了一个简单的Mock数据接口,读取 json 模板数据并返回,但使用 json.load 方法将 str 转\'json\'的过程中却遇到 AttributeError: \'str\' object has no attribute \'read\' 的错误,下图是详细的错误信息:

真是一 s 之差,谬之千里啊,如果你也遇到了同样的问题,快看一下是不是和我一样粗心,用错方法了。

如果这个方案解决了您的问题,请帮忙点赞♥️,让其他同学更直观的看到这个解决方案是有效的。

参考1: https://blog.csdn.net/lming_08/article/details/51799076

如何解决 AttributeError:模块 'tensorflow.compat.v2' 没有属性 'py_func'

【中文标题】如何解决 AttributeError:模块 \'tensorflow.compat.v2\' 没有属性 \'py_func\'【英文标题】:How to solve AttributeError: module 'tensorflow.compat.v2' has no attribute 'py_func'如何解决 AttributeError:模块 'tensorflow.compat.v2' 没有属性 'py_func' 【发布时间】:2021-10-15 05:59:31 【问题描述】:

def aurocc(y_true, y_pred): return tf.py_func(roc_auc_score, (y_true, y_pred))

adam = keras.optimizers.Adam(lr=0.0001) model.compile(optimizer=adam, loss='categorical_crossentropy',metrics=[aurocc]) model.fit(inputs,labels,validation_split=0.33,epochs=10,verbose=1,callbacks=callbacks)

AttributeError:在用户代码中:

/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:830 train_function  *
    return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:813 run_step  *
    outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:775 train_step  *
    self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:457 update_state  *
    metric_obj.update_state(y_t, y_p, sample_weight=mask)
/usr/local/lib/python3.7/dist-packages/keras/metrics.py:169 decorated  *
    update_op = update_state_fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/metrics.py:155 update_state_fn  *
    return ag_update_state(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/metrics.py:641 update_state  *
    matches = ag_fn(y_true, y_pred, **self._fn_kwargs)
<ipython-input-46-e104431197fe>:2 aurocc  *
    return tf.py_func(roc_auc_score, (y_true, y_pred))

AttributeError: module 'tensorflow.compat.v2' has no attribute 'py_func'

【问题讨论】:

欢迎来到 Stack Overflow。请阅读How to Ask 和meta.***.com/questions/261592/…。 【参考方案1】:

此名称 tf.compat.v1.py_func 在 TF2 中已被弃用并删除,但您可以改用 tf.numpy_function

之前:(在 TensorFlow 2.x 中显示警告)

def fn_using_numpy(x):
  x[0] = 0.
  return x
tf.compat.v1.py_func(fn_using_numpy, inp=[tf.constant([1., 2.])],
    Tout=tf.float32, stateful=False)

输出:

WARNING:tensorflow:From <ipython-input-4-2c02087a506a>:5: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, there are two
    options available in V2.
    - tf.py_function takes a python function which manipulates tf eager
    tensors instead of numpy arrays. It's easy to convert a tf eager tensor to
    an ndarray (just call tensor.numpy()) but having access to eager tensors
    means `tf.py_function`s can use accelerators such as GPUs as well as
    being differentiable using a gradient tape.
    - tf.numpy_function maintains the semantics of the deprecated tf.py_func
    (it is not differentiable, and manipulates numpy arrays). It drops the
    stateful argument making all functions stateful.
    
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0., 2.], dtype=float32)>

之后:

tf.numpy_function(fn_using_numpy, inp=[tf.constant([1., 2.])],
    Tout=tf.float32)

输出:

<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0., 2.], dtype=float32)>

更多详情,请参考link。

【讨论】:

以上是关于解决 AttributeError: 'str' object has no attribute 'read'的主要内容,如果未能解决你的问题,请参考以下文章

解决:AttributeError: module 'requests' has no attribute 'get'”

解决编码问题:AttributeError: 'str' object has no attribute 'decode'

解决AttributeError:模块'pandas'没有使用command-prompt或pycharm属性'core'

python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'

unittest中报错:AttributeError: 'TestLogin' object has no attribute 'driver'解决方法

解决:pipenv shell报错:AttributeError: 'module' object has no attribute 'run'