python中rstrip用法_python lstrip,rstrip 的 用法

本文介绍了两种在Python中处理JSON数据的方法。方法一是通过json模块的loads()函数将JSON字符串转换为字典,首先去除字符串两侧的括号,然后进行转换。方法二是使用内置的eval()函数直接执行JSON字符串,将其转化为Python对象。这两种方法在处理JSON数据时非常实用。
摘要由CSDN通过智能技术生成

#方法一:

import json

s = '({"freight":[{"beforeSpecifiedTime":false,"cityCode":"","cityName":"","commitDay":60,"company":"CAINIAO_STANDARD","companyDisplayName":"AliExpress Standard Shipping","coreZone":"","currency":"USD","cutTime":"17:00","deliveryDate":"2019-09-20","deliveryDateDisplay":"other","discount":93,"discountType":"about","domesticFreight":"","errorCode":1,"errorDisplayMsg":"","isCheapestFaster":false,"isDefault":true,"isPromote":false,"isTracked":true,"localCurrency":"USD","localPrice":"0.35","localPriceFormatStr":"US $0.35","localSaveMoney":"5.02","localSaveMoneyFormatStr":"US $5.02","localTotalFreight":"5.37","localTotalFreightFormatStr":"US $5.37","logisticsDeliveryTimeType":"BUYER_PAY_ORDER","notification":"","price":"0.35","priceFormatStr":"US $0.35","processingTime":5,"promoteInformation":null,"remainTime":"-9475079","saveMoney":"5.02","saveMoneyFormatStr":"US $5.02","sendGoodsCountry":"CN","sendGoodsCountryFullName":"China","status":"","templateDiscount":"","templateType":"","time":"22","totalFreight":"5.37","totalFreightFormatStr":"US $5.37"},{"beforeSpecifiedTime":false,"cityCode":"","cityName":"","commitDay":60,"company":"EMS_ZX_ZX_US","companyDisplayName":"ePacket","coreZone":"","currency":"USD","cutTime":"17:00","deliveryDate":"2019-09-19","deliveryDateDisplay":"other","discount":74,"discountType":"about","domesticFreight":"","errorCode":1,"errorDisplayMsg":"","isCheapestFaster":false,"isDefault":false,"isPromote":false,"isTracked":true,"localCurrency":"USD","localPrice":"1.04","localPriceFormatStr":"US $1.04","localSaveMoney":"2.98","localSaveMoneyFormatStr":"US $2.98","localTotalFreight":"4.02","localTotalFreightFormatStr":"US $4.02","logisticsDeliveryTimeType":"BUYER_PAY_ORDER","notification":"","price":"1.04","priceFormatStr":"US $1.04","processingTime":5,"promoteInformation":null,"remainTime":"-9475079","saveMoney":"2.98","saveMoneyFormatStr":"US $2.98","sendGoodsCountry":"CN","sendGoodsCountryFullName":"China","status":"","templateDiscount":"","templateType":"","time":"21","totalFreight":"4.02","totalFreightFormatStr":"US $4.02"}]})'

#注意这个s是字符类型的

s1 = s.lstrip('(') #去除左括号

s = s1.rstrip(')') #去除右括号

s_dic = json.loads(s) #将字符串转成字典

print(s_dic)

#方法二:

使用eval()进行处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值