python - 从 Python 中的变量中删除字符
问题描述
我一直在使用天气网站 Open Weather Map ( https://openweathermap.org/ ) 来获取 Python 项目的详细信息。
rain = w.get_rain() # Get rain volume {'3h': 0}
wind = w.get_wind() # Get wind degree and speed {'deg': 59, 'speed': 2.660}
humidity = w.get_humidity() # Get humidity percentage 67
然后将它们插入到不喜欢'{''}'的数据库中。
我已经尝试从输出中删除“{”“}”,查看来自该站点的回复。跳闸:
rain = rain.strip(['{','}'])
并替换:
rain = rain.replace('{', ' ')
但我得到的只是“AttributeError:'dict'对象没有属性”。是否有另一个转义子句可以用于变量以删除不需要的字符?
解决方案
返回一个 python 字典,您需要访问键和值而不是删除 {}。下面显示了如何访问字典值。
print(rain.keys()) # returns a list of all dictionary keys
>>> ['3h']
print(rain['3h']) # returns the value associated with the '3h' key
>>> 0
推荐阅读
- java - Java 密钥库未能通过 PKCS12 完整性检查,但 keytool 有效
- jquery-ui - 可调整大小的 Jquery 边界
- angular - angular - 仅为使用它们的单个组件导入 3D 派对库样式
- javascript - 无法使用 Javascript 作为 BSF 采样器中的选定语言在 Jmeter 中发送 HTTP Post 请求
- javascript - 复选框在第一次单击时不会向 Vue 对象传递任何内容
- amazon-web-services - 如何验证 AWS 实例是否具有静态 IP
- python - PrettyPrinter - 在开始和结束括号的不同行上打印输出
- abap - 如何通过 BAPI 创建工单和分配通知?
- postgresql - 在 Spring Boot 中 jdbcTemplate 连接泄漏到 postgresql db
- c# - 在 EmguCV 中迭代 Mat 的每个像素