@MaratGan

Как исправить код, чтобы выдавала цену?

Добрый день!

Помоги решить следующую проблему.

Есть код
import json

def handle(data):
    data = json.loads(data)
    r = data['otvet1']
    s = json.loads(r)['parts']
    otvet1 = ''
    d  = {i: elem['brand'] for i, elem in enumerate(s)}
    for e in d.items():
        otvet1+= str(e[0]) + '. ' + e[1] + '\n'
    return otvet1


Беру данные вот от сюда:
spoiler

{"parts":[{"article":"GB1219","brand":"BIG FILTER","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 BIG FILTER GB1219 FORD Focus C-MAX 2.0 TDCi Mondeo IV 07-, CITROEN C2 C3 C4","stock":4,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":209,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"ky7zn\/UoLjZK10fBY\/5I8Fq5RTdKlkCvfXv03cUIF2hUq1rGigB3Nc+LBH6hLP4uaGhyTLOk0CNomuplIFAZyt4KNc3HyJJNdr+r\/h9q3WxWE+V0dT5G55iWWctl2E+U7ybpKndPqom0brUpwuRzChl+L+IHkSSzdl16yGW0ykgrYrYl7g5Rn4q+1maif4WdkG1gSmUJJF59LHpoHsDZ16UuhR8J9XGPgDbjCOd5mIOrhDKrZ298dCnN6MK2jAspVn8gvUR9+2xUqsJ5hYvzygEtcz1e9R9+6l\/YYNMU5H3hnrXFcyI\/03tOZZAyxQ50OmUjtWYQMG0sTPN7UecDY0QFCRnrGhTIelqq0WeBzsyU1F\/QVBceGQzgOPlVfmBmtoMbzkcs1pS5rtpL\/BlcUHSQHmh5SoqK+w1y\/TV5+r9Es9oTKw68hPYON4ZCmkHiinWclIjgxmT\/X5BehPgdOq\/Allg3q6G2n3ZpAu785bp6PBxrN6iGbGa5EeLr+mCe66WelQ0dgiIqzE8FsLsdXCxCDpAvB1q68Ma2KvDNEr8oVKL6wdg="},{"article":"1457429249","brand":"BOSCH","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 BOSCH 1457429249 FORD FOCUS\/MONDEO\/TRANSIT 2.0D-3.2D\/PEUGEOT 206-407 1.1-2.0","stock":50,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":292,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"sl6wyskNdf+pfnyHIoXwdtdHdNmZBATSlbuJClL3UTKVuvtGqf3JEPfjEvgw+EGgCdQ20SQx6Qx4hlCL1oP6Hc0VMM8FoNjS6ADe5k1\/ICsSzqHr83nLhXphrtdrQD4CH7btxFmctGniNXmSmS6oU8rxFF5ZO4iYfMnx3pZynqmcDgtOyJVDgi1YuW+fynosUut3HzsQ0URB0V9P13rt21Nl2Za60H9YJsGeZvu\/spP3NuNtOU+AHEMTj42vwb1rht9HBUgAWnzH0Wwu2UtQpiypSE7JDrmfeLq4dVZTqaQro\/+WPoaii+U0YaalQAPzM+YYMv6Btm+Fhc9MWCDNFmuRNfTazHfKugU++yG\/jZazd90mvwxEmZ5cIoIiAQ2SftOg6PiVfL+RAqi8odVXAjMqEvCAIxI6lDVebqjXaieOxYCvVUPVogmD+NwalpPJ3O\/WpCau7lhFcQFjmNe7ZItH13lglh9FvQEg7UuR37qOkc1\/\/eCVr\/sJxI9ILAhJCWGonVsxSv+UXOx1FLQq51XMeqfqaM2fbFFypQzhJMaSp0JQs8l9XwB+"},{"article":"OE673","brand":"FILTRON","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 FILTRON OE673 FORD Focus II 2.0TDCi,VOLVO S40\/V50 2.0D","stock":101,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":238,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"nRylPU\/zjmCBwS1vt87f4k5+XrxuYKBKJkHtlFWU2NpscdzODZVKGt54JcJMwD9LAb1P\/grsSv+K2+yhLlrY\/NX22aEpAvpEeW6MI3ThvjVq\/+WElw4L8RIYwogJhvm55mOcPCYO7Mzqs3wqj4c4v8PYH\/Ot1hDfav6BJKU7eSJ3aY4dL08naOoiJrGn2cTmG3OEfJT9+Y9w3\/QVMPknThlThtpscM\/goWTU7wkqaenlpE654eipMOrvZOmzr7+8WUPGY+gywFDBCIvumyu8iq5PVJBCTnIeQ6DnPjdxX3vO417Qt8AFNr5A7t3ZZYsG\/aM40I0TSzm\/1vHrHhPa5Fl+M3tKCF\/g9u4MDYTYYZ29eW9fv6qcXaRc9OfCr\/mJ8l1Ov6T9RrqjU+SunhrJfebrDp\/c4tYy9bJSEXMhW0Dec01CgR6NardDBTQxUEtW31EyNATFTfzlwj0Kqzumic4LCrASk9SuUapXPfJxm9DqA8ULhN2Cjmkj7D4K6oYdIssh\/64r7cXGIMoTUfza3A=="},{"article":"OE406J","brand":"JS ASAKASHI","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 JS Asakashi OE406J CI C3 Pluriel\/C3 XTR 05-, C4\/C5 04-, C1\/C8 05-, C2\/C6 06-, Jumpy\/Dispatch 1.6\/2.0L 07-, C-Crosser 2.2L 07-, FI Scudo 1.6\/2.0L 07-, FO Kuga 2.0L 08-, Mondeo 2.0\/2....","stock":5,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":209,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"99hYA2\/gGOrK+bzT186nUVF48OUqKW3pM9Ydwl9cNpperIVDS4qLAt2xws1sEinDS\/pnjCzAsI1FgMcTKMdolLpZe0w7ydALMcYV07IHmekrwAHHlVIiV15tG+xO386h63XT2baI1g26uIrAEilCzsWh47wxKg002XPL0KK\/Ypi4cMq273z80iU6KQSwtGzrpdmYLAiJh8uPuibYZ3ampx5+ygGPPXG0fcLu3inUYxnft4VbaVfufZuZ3lqVdjmKHJtoH4SUWyFSZ\/idR2Lv1zsrsCZpBldY7urx1V5GZSWDGNgd15iofc5Zyl80fsiJmoVdfS4hq\/ecAC1UNEnzQ9igd2nmVZNG2rQzYPDXyYNwf8KcoquTcVV7bseKrDnmDtLDzkkdT7bEcl4p5b6ENuPreiNQ4eapr9xJjwcD0bEksOpntXvaTo\/Digj1B\/UQTtF9J1hLVKALKQ52osd1p40uy7pdCbVRFB2IRR5irWYTXeGdy+8n03ZWGkOjqc4z6MbzKAD3I\/HmxLDgWJyZABEkDn0EhqdaJwXTdyGqQkosL2CwYqfAOqZhxkHE4r84vcu7HDNoyyZs\/RhjfD9aPXMXsl13lQ5kQ4FYGfMp\/hvDewzu6Fe\/vyzcCYpM\/8ghrGFMTn5gfSryc+pK4wAun6oKfKa1z7Kyq69st3D9QnwLUp\/y7nOC2GV9msQ5QVx2xbk42Fviem6rAHAq"},{"article":"OE42001","brand":"JS ASAKASHI","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 JS Asakashi OE42001 FORD Focus II 2.0TDCi,VOLVO S40\/V50 2.0D","stock":8,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":214,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"uuq0l5+C28xGlLMjsw3wNpv5qanQLk4nuACsj14BHzqf5ou57lQ328zmneekTBE8OpcS5q1qXB06bP5Wp3uVshtiA1fCnn6lgIgQPaAdaWUCxwmdoJPYAvUPhwZpB+4+LqRkPdsgVj++k1x7BAhpzQsiVPNHK1JZHl5EF8eq9\/c8au8gG7udp8R9\/coF8mEPznKHfK4e\/oZQRpH91AyHPvOKIGJZi3pTb\/4xdI\/hopdhf\/X9e6e3Lc0Qc78FJfkI8hjtirKdfPU8zm5mgEoi2dlpJeNs44FNTiDd1\/QTSljn+FZM0\/lFp+ijbia2TiwwxU8yzM+xDpZo4NCqkMkC2hji1ZfFFIED3bAAV97MJ2yqgnkWhCMzSbiynr7zwy8tlDX\/aAUYIKtZAhSrRMGYd+hWSDV2BMq7qW6nztmuvnd6F2nNV+YcE64Rbk13iLwiU1k0+Mk+C1JDJJIx1vAiO7Yvl4p5HlYJagZX\/mBVqchA8Wm7E1utba5+fspRbFUPf2OKYoPX8iBE78CptxqoL9BeEO0we7\/CdFA="},{"article":"HU71151X","brand":"MANN-FILTER","name":"\u0424\u0438\u043b\u044c\u0442\u0440 \u043c\u0430\u0441\u043b\u044f\u043d\u044b\u0439 MANN HU71151X Samand 1.6 \/ FORD Focus II Peugeot 207, 307, 308, 308 II, 3008, Boxer III, Partner II, Citroen Berlingo I,II, C3, C3 II, C4, C4 II, DS3, DS4","stock":101,"multiplicity":1,"deliverytime":{"min":0,"max":0},"percent":100,"price":336,"warehouse":"CHEL","returnable":false,"return_cost":0,"comment":null,"date_added":"2020-02-28","unsafe":false,"to_cart":"RnbEcoqvctH4aaaV9kmdUaKH0Sysbo61ayFlP2Z+LqG4MAu5yUNmtjRhAJdP755+k3lecklbAv9ep4QKMjz+tXb2VEWebbCQdc9Qj0C8x2Hl5ItXLQPZIM4XGbTWEdmSwfbnTd8Q0E2rZFrh+4vOq6h\/M8kKxy\/YPQmPDMrB5lNv3X83db5DuAOx4pew6Ugy2PErUVSPRR9Sh8Kw3YQum\/4UVFX2qJzHNSylWaAsJ9lv\/jCRn4UQBXxcAFw42Uq99HpWyvRMFyJSV3wGtZ8amqCUGTZnqIGsaMIutznTgWBK\/wd0vYKU4WY6zYwly7NIwCHij\/wUk41P6mXbsydtIWeqM+t2Hcaz0YNKDY1zwRkLFGybwDFcuw+KfQvn0XeUbir7fl6LfMKhIb9fTDeUdLZtvgDDcLzyfUoYXI\/A8tB0ZtMpI7l8feitYReC4d\/rWfi9HYlXP57nLnoxJP4TYt9EzgSscf1bVmYhN1FCe7disiKmBhJLvS6VI2\/0coISjUpkfrybGfAMweWE5lNTcos8Z4WwlnR7OobmoKvEyzVmMn76LpwryjjuT\/OZ5Oa1qxLWL6yayMwLQm9e\/Ik\/z3h0DOLD\/0jz5+Hf8Zzj2FqAQd8kMkyh4Zes\/wc0BueziUzFdl+U4vS6D8gYasordHunKX6MGliNazHg"}]}


Результат получается вот такой:

0. BIG FILTER
1. BOSCH
2. FILTRON
3. JS ASAKASHI
4. JS ASAKASHI
5. MANN-FILTER
6. MILES
7. NIPPARTS
8. SAKURA
9. ZEKKERT

А мне нужно, чтобы был такой, т.е подставлялось значение из "price" и добавлялось "руб.":

0. BIG FILTER 209 руб.
1. BOSCH 292 руб.
2. FILTRON и т.д.
3. JS ASAKASHI и т.д.
4. JS ASAKASHI и т.д.
5. MANN-FILTER и т. д.
6. MILES и т.д.
7. NIPPARTS и т.д.
8. SAKURA и т.д.
9. ZEKKERT и т.д.
  • Вопрос задан
  • 150 просмотров
Решения вопроса 1
@Xander017
У меня работает вот так:

def handle(data):
    for i, w in enumerate(data['parts']):
        print(str(i)+("."), w['brand'], w['price'], "руб.")

Out:
0. BIG FILTER 209 руб.
1. BOSCH 292 руб.
2. FILTRON 238 руб.
3. JS ASAKASHI 209 руб.
4. JS ASAKASHI 214 руб.
5. MANN-FILTER 336 руб.


В return вместо printпопробуй переделать сам (если надо).
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы