将str转换为float(python34)

2024-05-28 21:08:00 发布

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

在我的python脚本中有一部分我收到了这个错误:

TypeError:不支持+:“float”和“str”的操作数类型

代码:

for proj in data['daily_projections']:
  proj['nba_player_id'] = float(proj['nba_player_id'])
  print(proj['fanduel_fp'] + ' ' + proj['nba_player_id'])

这是我目前拥有的,它不能正常工作。你知道吗

“proj['fanduel\u fp']”是浮点数,“proj['nba\u player\u id']”是需要转换为浮点数的字符串


Tags: 代码脚本id类型for错误floatproj
2条回答

打印时将proj['nba_player_id']proj['fanduel_fp']转换为字符串数据类型。你知道吗

for proj in data['daily_projections']:
  proj['nba_player_id'] = float(proj['nba_player_id'])
  print(str(proj['fanduel_fp']) + ' ' + str(proj['nba_player_id']))

您可以使用不需要显式转换的^{}

print('{0[fanduel_fp]} {0[nba_player_id]}'.format(proj))

>>> proj = {'nba_player_id': '1', 'fanduel_fp': 2}
>>> '{0[fanduel_fp]} {0[nba_player_id]}'.format(proj)
'2 1'

相关问题 更多 >

    热门问题