Traceback (most recent call last): File ~/anaconda3/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec exec(code, globals, locals) File ~/stock/daily_analyse.py:65 a = next_net_mf_amount - net_mf_amount TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'
时间: 2024-02-11 17:05:46 浏览: 171
这个错误提示是在执行 `next_net_mf_amount - net_mf_amount` 时出现了问题,其中 `next_net_mf_amount` 的值为 `NoneType` 类型,而 `net_mf_amount` 的值为 `float` 类型,这两种类型之间无法进行减法运算。
解决这个问题的方法取决于你的具体需求。如果 `next_net_mf_amount` 可能为 `None`,则需要在进行减法运算前先判断其是否为 `None`。例如:
```python
if next_net_mf_amount is not None:
a = next_net_mf_amount - net_mf_amount
else:
a = None # 或者使用其他默认值
```
如果 `next_net_mf_amount` 不应该为 `None`,则需要检查代码中为什么会出现 `None` 值,例如可能是调用了某个函数返回了 `None`,或者是变量赋值出现了问题等等。需要对代码进行排查和调试。
相关问题
输出报错runcell(0, '/Users/mac/??????.py') Traceback (most recent call last): File ~/anaconda3/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec exec(code, globals, locals) File ~/??????.py:36 df2=df2.sort_values(by='??/?',ascending=False) File ~/anaconda3/lib/python3.10/site-packages/pandas/util/_decorators.py:331 in wrapper return func(*args, **kwargs) File ~/anaconda3/lib/python3.10/site-packages/pandas/core/frame.py:6912 in sort_values k = self._get_label_or_level_values(by, axis=axis) File ~/anaconda3/lib/python3.10/site-packages/pandas/core/generic.py:1850 in _get_label_or_level_values raise KeyError(key) KeyError: ‘人数/万’,重新修改一下这个程序
根据错误信息看来,出现了`KeyError`异常,即没有找到名为`'人数/万'`的列。你可以检查一下DataFrame是否包含该列,或者该列名是否拼写正确。另外,为了避免出现中文乱码问题,可以在文件开头添加如下代码,设置编码为UTF-8:
```python
# -*- coding: utf-8 -*-
```
根据你提供的代码,修改后的程序如下:
```python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.gk100.com/read_16892539.htm'
header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15"}
r = requests.get(url, headers=header)
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, features="lxml")
w1 = soup.find_all("td")
result = w1
df = pd.DataFrame(result)
df1 = df[2:]
for index in df1:
d1 = df1[df1.index%2==0]
d2 = df1[df1.index%2!=0]
d1 = d1.reset_index(drop=True)
d2 = d2.reset_index(drop=True)
df2 = pd.concat([d1,d2], axis=1)
# 排序,按照第二列的元素进行降序排列
df2 = df2.sort_values(by='??/?', ascending=False)
title = ['省份', '人数/万']
df2.columns = title
print(df2)
```
需要注意的是,你的代码中`by='??/?'`的部分中文字符被替换成了问号,可能是文件编码不太兼容,你需要手动修改为正确的列名。
(64849, 21) (64849, 1) trainX Y shape is: (64599, 48, 21) (64599, 60, 21) (64599, 1) Traceback (most recent call last): File ~\anaconda4\envs\tensorflow\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File d:\lstnet_keras-master\train_batch_a1.py:148 model,normalize = startTrainMult_1H(data,name,config) File D:\lstnet_keras-master\lstm\LSTNet_Interface_a38.py:570 in startTrainMult_1H model = trainModel(trainX1, trainX2 , trainY, config) File D:\lstnet_keras-master\lstm\LSTNet_Interface_a38.py:500 in trainModel model = LSTNetAttention(trainX1,trainX2,trainY,config) File D:\lstnet_keras-master\lstm\LSTNet_Interface_a38.py:443 in LSTNetAttention lstm1 = LSTM(64, return_sequences=True)(input1) File ~\anaconda4\envs\tensorflow\lib\site-packages\keras\layers\recurrent.py:541 in __call__ return super(RNN, self).__call__(inputs, **kwargs) File ~\anaconda4\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py:75 in symbolic_fn_wrapper return func(*args, **kwargs) File ~\anaconda4\envs\tensorflow\lib\site-packages\keras\engine\base_layer.py:475 in __call__ previous_mask = _collect_previous_mask(inputs) File ~\anaconda4\envs\tensorflow\lib\site-packages\keras\engine\base_layer.py:1441 in _collect_previous_mask mask = node.output_masks[tensor_index] AttributeError: 'Node' object has no attribute 'output_masks'
这个错误是由于 Keras 版本的问题导致的。在旧版本的 Keras 中,LSTM 层的参数是 return_sequences 和 return_state,而在新版本的 Keras 中,LSTM 层的参数是 return_sequences、return_state 和 return_attention。你可以检查一下你使用的 Keras 版本是否与代码中使用的版本相同。如果不同,你需要修改代码以适应你的 Keras 版本。另外,你可以尝试在 LSTM 层中添加 `return_sequences=True` 参数来解决这个问题。
阅读全文