python - 将字典列表中的字符串更改为整数
问题描述
我的列表如下所示:
print(dataset[0:2])
这使:
[{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'}, {'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'}]
但是我想将“cases”和“population2019”中的值更改为整数而不是字符串。
我已使用此代码将字符串更改为整数
for i in dataset: #Changes the value of multiple keys to integers instead of string
for key in i:
i['cases'] = int(i['cases'])
i['population2019'] = int(i['population2019'])
它确实将“cases”的值更改为整数而不是字符串,但它不适用于“population2019”,因为我认为它是一个大整数(“38041757”)。
错误是:
ValueError: invalid literal for int() with base 10: ''
解决方案
我相信你有这个错误,因为population2019
当你尝试转换它时是一个空字符串 ('')
推荐阅读
- sql - 将前一行的值添加到当前行
- c# - dotnet Core - 需要创建一个具有两个泛型和一个是扩展的泛型扩展方法
- python - 如何使用 Microsoft Enterprise Library (Security.Cryptography) 生成的密钥 (*.key 文件) 在 python 中加密?
- azure-ad-b2c - Azure B2C - 自定义策略 - 自定义 UI - 更改密码 _ 继续按钮禁用
- artifactory - 使用简单默认布局的通用仓库的 Artifactory 版本搜索
- python - 将每个字母与文本分开,而不将同一个字母保存两次
- flash - FancyUpload - 替换 Flash
- java - 将 API 请求对象作为空抽象或接口?
- mysql - 这个外键约束有什么问题
- c# - 我的 XUnit dotnet 核心项目构建在我的桌面上,但不在 DevOps Server 2019 中