我有一个vehicle['estimatedCalls']['estimatedCall']
列表,其中包含以下内容:
[
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24684:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24683:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24680:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24687:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24686:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24685:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
}
]
我希望遍历每个stopPointRef
,expectedDepartureTime
一对(此处不存在,有时expectedArrivalTime
,aimedDepartureTime
和aimedArrivalTime
存在时),检索其值(对于stopPointRef
,值不是value
,而是第二项(以STIF:StopPoint:Q:
开头)。
这是我目前的代码:
for call in vehicle['estimatedCalls']['estimatedCall']:
stoptime = ent.trip_update.stop_time_update.add()
for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']):
stoptime.stop_id = vehicle['estimatedCalls']['estimatedCall']['stopPointRef']['value']
stoptime.arrival_time = call['expectedArrivalTime']
stoptime.departure_time = call['expectedDepartureTime']
" for"循环似乎正常工作(print vehicle['estimatedCalls']['estimatedCall']
返回正确的列表)
但在尝试迭代每个stopPointRef
,expectedDepartureTime
组时,请执行以下操作:
for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']):
我收到以下错误:TypeError: list indices must be integers, not str
你能帮我解决这个问题并找到合适的代码来执行吗?谢谢你的帮助!
答案 0 :(得分:1)
为简单起见,根据提供的信息,这里有一个示例
var response = {
"id": "15ade50437b9aa01",
"threadId": "15ade50437b9aa01",
"labelIds": [
"UNREAD",
"IMPORTANT",
"SENT",
"INBOX"
],
"snippet": "",
"historyId": "1171380",
"internalDate": "1489788486000",
"payload": {
"mimeType": "multipart/related",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/related; boundary=94eb2c034184892a95054af46913"
}
],
"body": {
"size": 0
},
"parts": [
{
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=94eb2c034184892a93054af46912"
}
],
"body": {
"size": 0
},
"parts": [
{
"partId": "0.0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
}
],
"body": {
"size": 25,
"data": "W2ltYWdlOiBJbmZvZ2FkIGJpbGQgMV0NCg=="
}
},
{
"partId": "0.1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}
],
"body": {
"size": 106,
"data": "PGRpdiBkaXI9Imx0ciI-PGltZyBzcmM9ImNpZDppaV8xNWFkZTUwMmVlYTg0MGNlIiBhbHQ9IkluZm9nYWQgYmlsZCAxIiB3aWR0aD0iNTgiIGhlaWdodD0iNTQiPjxicj48L2Rpdj4NCg=="
}
}
]
},
{
"partId": "1",
"mimeType": "image/png",
"filename": "smile.png",
"headers": [
{
"name": "Content-Type",
"value": "image/png; name=\"smile.png\""
},
{
"name": "Content-Disposition",
"value": "inline; filename=\"smile.png\""
},
{
"name": "Content-Transfer-Encoding",
"value": "base64"
},
{
"name": "Content-ID",
"value": "\u003cii_15ade502eea840ce\u003e"
},
{
"name": "X-Attachment-Id",
"value": "ii_15ade502eea840ce"
}
],
"body": {
"attachmentId": "ANGjdJ8Xh1_0DBjFbc2qKRHD8uTw-9nkPP30v-vohJforDg54EHPHf3Obd2P9W6Wfss0cwfmblQWi5F3958vcEi0HyiMNgpKJbsQAVP9viUOY4LzyxwAvR7-dis4PNGflBpkZFMHv62LGKkQ1-ZPG3Go_Xh_sXJUveHl4JjmwLpNp6LjlHzuA_3XOkY2LLQLFmXNTo_dJbqDQWvMb8UTGnATMOoTNKvNQ4Ndr9pgQYI1SBvtdThgUDmlOGKYLHM6qR4AlrNNFnPUCZZU-BB7o7Dt2dhj-kexiIdvaB2LEnoeCBth_oK9HELt2tw4rlY",
"size": 8539
}
}
]
},
"sizeEstimate": 12800
};
function getHtml(res) {
var parts = [res.payload];
while (parts.length) {
var part = parts.shift();
if (part.parts) {
parts = parts.concat(part.parts);
}
if(part.mimeType === 'text/html') {
return decodeURIComponent(escape(atob(part.body.data.replace(/\-/g, '+').replace(/\_/g, '/'))));
}
}
return '';
}
function getAttachmentId(res, cid) {
var parts = [res.payload];
while (parts.length) {
var part = parts.shift();
if (part.parts) {
parts = parts.concat(part.parts);
}
var headers = part.headers;
var indexedHeaders = headers.reduce(function(acc, header) {
acc[header.name.toLowerCase()] = header.value;
return acc;
}, {});
var contentId = indexedHeaders['content-id'] || '';
var xAttachmentId = indexedHeaders['x-attachment-id'] || '';
if (contentId.includes(cid) || xAttachmentId.includes(cid)) {
return part.body.attachmentId;
}
}
return '';
}
var html = getHtml(response);
console.log(html);
// Extract the cids and find the matching attachments in the response
var attachmentId = getAttachmentId(response, 'ii_15ade502eea840ce');
console.log(attachmentId);
// Get the attachment from the Gmail API and replace the cid
// with the base64-data