-1

Here is my code. It is necessary to output with preservation of text formatting and changes made to the dictionary

from pprint import pprint
site = {
    'html': {
        'head': {
            'title': 'Куплю/продам телефон недорого'
        },
        'body': {
            'h2': 'У нас самая низкая цена на iphone',
            'div': 'Купить',
            'p': 'продать'
        }
    }
}
def find_key(struct, key, meaning):
    if key in struct:
        struct[key] = meaning
        return site
    for sub_struct in struct.values():
        if isinstance(sub_struct, dict):
            result = find_key(sub_struct, key, meaning)
            if result:
                return site
number_sites = int(input('Сколько сайтов: '))
for _ in range(number_sites):
    product_name = input('Введите название продукта для нового сайта: ')
    key = {'title': f'Куплю/продам {product_name} недорого', 'h2': f'У нас самая низкая цена на {product_name}'}
    for i in key:
        find_key(site, i, key[i])
    print(f'Сайт для {product_name}:')
    pprint(site)

Doesn't show full dictionary

3Vw
  • 53
  • 7

3 Answers3

1
#import json

data = json.dumps(site, ensure_ascii=False, indent=4)
print("site = " + data.replace('"', "'"))

That's how perfect it was

3Vw
  • 53
  • 7
0

This should work

import json

print(json.dumps(site, indent=4, ensure_ascii=False))

  • Sorry. Static data is not json. This is a piece of code that needs to be changed when entering data and output the result as in the picture. – 3Vw Apr 05 '22 at 22:11
  • https://imgur.com/a/6pkF24b This is what the conclusion should be. Is it possible to achieve such a result? – 3Vw Apr 05 '22 at 22:15
  • I did not immediately understand, your variation is 100% suitable thanks – 3Vw Apr 06 '22 at 12:49
0

Possible solution is the following:

import json

number_sites = int(input('Сколько сайтов: '))

for _ in range(number_sites):
    site = {'html':{
        'head':{'title': 'Куплю/продам телефон недорого'},
        'body':{'h2': 'У нас самая низкая цена на iphone','div': 'Купить', 'p': 'продать'}}}
    product_name = input('Введите название продукта для нового сайта: ')
    key = {'title': f'Куплю/продам {product_name} недорого', 'h2': f'У нас самая низкая цена на {product_name}'}
    
    site.get('html', {}).get('body', {})['h2'] = key['h2']
    site.get('html', {}).get('head', {})['title'] = key['title']
    
    print(f'\nСайт для {product_name}:')
    print('site =')
    print(json.dumps(site, indent=4, ensure_ascii=False))

Returns

Сколько сайтов: 1
Введите название продукта для нового сайта: IPHONE

Сайт для IPHONE:
site =
{
    "html": {
        "head": {
            "title": "Куплю/продам IPHONE недорого"
        },
        "body": {
            "h2": "У нас самая низкая цена на IPHONE",
            "div": "Купить",
            "p": "продать"
        }
    }
}
gremur
  • 1,645
  • 2
  • 7
  • 20
  • You improved and shortened my code. Thank you for that. But the output of the result remained unchanged. I need to achieve exactly the same output with all the brackets as an example below: – 3Vw Apr 05 '22 at 21:40
  • I'm sorry, I didn't understand. The `site` structure remained unchanged and all brackets are in place, only the values of the keys have changed. – gremur Apr 05 '22 at 21:46
  • https://imgur.com/a/56VnrOw I need to get this output But it's not showing up at the moment :( https://imgur.com/a/Y7RZJq8 – 3Vw Apr 05 '22 at 21:58
  • @3Vw, Please take a look at the updated code above – gremur Apr 05 '22 at 22:16
  • https://imgur.com/a/DsAXm5u It came out almost perfect. But those commas over there are superfluous and should not be displayed. Marked with arrows. – 3Vw Apr 05 '22 at 22:34
  • Can it be easier to do this somehow using recursion? – 3Vw Apr 05 '22 at 22:48