Python, как объединить динамические части шаблона HTML, а затем распечатать их как один HTMLPython

Программы на Python
Anonymous
Python, как объединить динамические части шаблона HTML, а затем распечатать их как один HTML

Сообщение Anonymous »


this is my template.py:

first = ''' This is header H1 ID Name Phone Email ''' uid='{uid}' second=''' ''' name='{name}' third=''' ''' phone='{phone}' fourth=''' ''' email='{email}' fifth=''' ''' I'm going to email this, but I think if I print as the expected output, it should work.

I have a list like this:

list1 = [ {'uid': 1, 'name': 'saeed1', 'phone': '+989100000000', 'email': 'sample1@gmail.com'}, {'uid': 2, 'name': 'saeed2', 'phone': '+989200000000', 'email': 'sample2@gmail.com'}, {'uid': 3, 'name': 'saeed3', 'phone': '+989300000000', 'email': 'sample3@gmail.com'}, {'uid': 4, 'name': 'saeed4', 'phone': '+989400000000', 'email': 'sample4@gmail.com'}, ] Expected output is:

This is header H1 ID Name Phone Email 1 saeed1 +989100000000 sample1@gmail.com 2 saeed2 +989200000000 sample2@gmail.com 3 saeed3 +989300000000 sample3@gmail.com 4 saeed4 +989400000000 sample4@gmail.com This is my attempt:

import template for n in list1: uid = template.uid.format(uid=n['uid']) name = template.name.format(name=n['name']) phone = template.phone.format(phone=n['phone']) email = template.email.format(email=n['email']) combined = template.first + uid + template.second + name + template.third + phone + template.fourth + email + template.fifth print(combined) I know this is not correct, and I think objects like template.first, template.second, etc. which are static should be outside the for loop, but I have not idea what to do next and how to combine them.


Источник: https://stackoverflow.com/questions/781 ... then-print

Вернуться в «Python»