在 for 循环中使用 Python .format(urlencode)

问题描述

我有一个带有一些 Id 作为值列表的字典。对于这个列表,我想使用一个循环来使用 .format 在 URL 中插入每个 ID 和键。

目标是使 URL 看起来像这样:

data = urllib.request.urlopen('https://lignumdata.ch/api/v1.cfc?method=getBauteil&type=all&condition={"**KEY**":"**ITEM FROM LOOP**"}').read()

我该如何继续?

我的代码:

myDict = {'={"id":'['1838A0BD-EFED-AC5F-110BFF3266C2A08C','1838C66B-0E3C-0073-A52F55BFEF1CDDD7','1838D369-FE2D-54FD-993706CE152D2A1D','18398B8C-0B6B-33A6-92BAA6FB216995F9','18399F2E-BCA9-0457-C28BAFA5F7466181','1839AD62-B125-1D7C-E4FD205840A3B7B4','183A43AD-9D1C-8121-D3FA5FB69C2BE2CF','183A53B2-CBB5-13B3-29B77034EABCE1C8','183A61F0-CD79-878B-EA9943B8C9EA12D0','183A692D-ED62-DA89-057A2630F061B29F','183AC5C6-BF39-0359-12C6FBB14392EAFC','183AD460-9F06-3841-E660630702C40991']}

结果应如下所示:

data = urllib.request.urlopen('https://lignumdata.ch/api/v1.cfc?method=getBauteil&type=all&condition={"id":"192E4CD9-B41B-09E3-1C2148F313A3A83B"}').read()

感谢您的帮助! :)

解决方法

在 python3 中,您可以使用 router.post('/ais',verifyBearerToken,async (req,res) => { //console.log(req.body); let codedMessage = req.body.message; const binaryMessage = decodeAisMessageToBinary(codedMessage); if(binaryMessage.length === 6 * codedMessage.length){ //console.log('Decoding to binary successful!'); const messageType = decodeMessageTypeFromBinary(binaryMessage); //console.log(messageType); let timestamp = req.body.timestamp; const mmsi = decodeMmsiFromBinary(binaryMessage); if (messageType === 'dynamic') { const navigationStatus = decodeNavigationStatusFromBinary(binaryMessage); const speedOverGround = decodeSpeedOverGroundFromBinary(binaryMessage); const positionAccurancy = decodePositionAccuracyFromBinary(binaryMessage); const position = decodePositionFromBinary(binaryMessage); const courseOverGround = decodeCourseOverGroundFromBinary(binaryMessage); const trueHeading = decodeTrueHeadingFromBinary(binaryMessage); const blueFlag = decodeBlueFlagFromBinary(binaryMessage); let dynamicMsg = new AisVesselPosition({ timestamp,mmsi,navigationStatus,speedOverGround,positionAccurancy,position,courseOverGround,trueHeading,blueFlag }); await dynamicMsg.save(); dynamicMsg = null; } else if (messageType === 'static') { let existingDataForMMSI = await AisVesselData.find({mmsi}); if(existingDataForMMSI.length === 0){ const name = decodeVesselNameFromBinary(binaryMessage); const size = decodeVesselSizeFromBinary(binaryMessage); const dimensions = decodeVesselDimensionsFromBinary(binaryMessage); let staticMsg = new AisVesselData({ timestamp,name,size,dimensions }); await staticMsg.save(); staticMsg = null; } existingDataForMMSI = null; } timestamp = null; } codedMessage = null; res.json({}); 语法来格式化字符串。将其与 Lambda 函数相结合可以定义格式模板。

还可以使用 f' ' 函数迭代字典的键。

dict.keys

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...