有没有办法一次进行多个不同的字符串更换?

发布于 2025-02-02 05:40:16 字数 1143 浏览 1 评论 0 原文

由于我们被排除在使用任何网络报废库之外

def print_ticket():
    if event.get() == 1:
        web_page = urlopen(url1)
        html_code = web_page.read().decode("UTF-8")
        web_page.close()
        event_title = findall('<h6.*>(.+)</h6>', html_code)[0]
        event_image = findall('<img.* src="([^"]+)".*>', html_code)[4]
        event_url = 'https://suncorpstadium.com.au/what-s-on.aspx'
        event_details = findall('<h7.*>(.+)</h7>', html_code)[1]
        filename = event_title.replace(' ', '_') + '_Ticket.html'
        html_file = open(filename, 'w')
        html_file.write(ticket_template.replace('EVENT TITLE', event_title + ' Ticket'))
        html_file.write(ticket_template.replace('IMAGE', event_image))
        html_file.write(ticket_template.replace('DATE TIME', event_details))

我正在做大学作业, 该事件在我的GUI中,我的Web文档打印了3个不同的模板集,每个部分都在一个部分上替换。

有没有一种方法可以立即制作多个 .replaces 而没有打印我的模板的多个副本?

I am doing a university assignment and since we are excluded from using any web scrapping libraries, I am limited to regex, I have the current code written (excuse the poor formatting, I am still very new):

def print_ticket():
    if event.get() == 1:
        web_page = urlopen(url1)
        html_code = web_page.read().decode("UTF-8")
        web_page.close()
        event_title = findall('<h6.*>(.+)</h6>', html_code)[0]
        event_image = findall('<img.* src="([^"]+)".*>', html_code)[4]
        event_url = 'https://suncorpstadium.com.au/what-s-on.aspx'
        event_details = findall('<h7.*>(.+)</h7>', html_code)[1]
        filename = event_title.replace(' ', '_') + '_Ticket.html'
        html_file = open(filename, 'w')
        html_file.write(ticket_template.replace('EVENT TITLE', event_title + ' Ticket'))
        html_file.write(ticket_template.replace('IMAGE', event_image))
        html_file.write(ticket_template.replace('DATE TIME', event_details))

My issue is, everytime I run that that event in my GUI, my web document prints 3 different sets of my template with the .write replaces occurring on one per section.

Is there a way to make multiple .replaces at once without it printing multiple copies of my template?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2025-02-09 05:40:16

问题在于您正在调用 Write 3次,您只需要调用一次即可。因此,您可以做什么:

        ticket_template = ticket_template.replace('EVENT TITLE', event_title + ' Ticket')
        ticket_template = ticket_template.replace('IMAGE', event_image)
        ticket_template = ticket_template.replace('DATE TIME', event_details)
        html_file.write(ticket_template)

这样它将起作用,您只会拥有 ticket_template 的最终输出。另外,您可以将其减少到单线,但看起来不会清晰

html_file.write(ticket_template.replace('EVENT TITLE', event_title + ' Ticket').replace('IMAGE', event_image).replace('DATE TIME', event_details))

The problem is that you are calling write 3 times and you need to call it just once. So what you could do:

        ticket_template = ticket_template.replace('EVENT TITLE', event_title + ' Ticket')
        ticket_template = ticket_template.replace('IMAGE', event_image)
        ticket_template = ticket_template.replace('DATE TIME', event_details)
        html_file.write(ticket_template)

in that way it will work, and you will only have the final output of the ticket_template. Also you can reduce this to a one-liner but it won't look legible

html_file.write(ticket_template.replace('EVENT TITLE', event_title + ' Ticket').replace('IMAGE', event_image).replace('DATE TIME', event_details))
千里故人稀 2025-02-09 05:40:16

您可以使用“ f-string”或文字 。为了控制其评估,必须指定它为从a “ nofollow noreferrer”> ) lambda 函数如下所示。
请注意,使用的变量名称不必为 all_caps ,如图所示 - 我只是这样做的,使其更容易发现使用它们的位置。

ticket_template = lambda: f'''\
Congratulations! Your ticket to {EVENT_TITLE} has been booked!
{IMAGE}
{DATE} {TIME}
'''

filename = 'whatever.html'

with open(filename, 'w') as html_file:

    EVENT_TITLE = 'Some event title'
    IMAGE = 'Picture of event'
    DATE, TIME = '29/05', '4:00 PM'

    filled_in_ticket = ticket_template()  # *Call* the lambda function.
    html_file.write(filled_in_ticket)

print('fini')

You can do it using an "f-string" or Formatted string literal which was introduced in Python 3.6. To control its evaluation, it must be specified as the result returned from a lambda function as shown in the sample code below.
Note that the variable names used do not have to be ALL_CAPS as shown — I only did it that way to make it easier to spot where they're being used.

ticket_template = lambda: f'''\
Congratulations! Your ticket to {EVENT_TITLE} has been booked!
{IMAGE}
{DATE} {TIME}
'''

filename = 'whatever.html'

with open(filename, 'w') as html_file:

    EVENT_TITLE = 'Some event title'
    IMAGE = 'Picture of event'
    DATE, TIME = '29/05', '4:00 PM'

    filled_in_ticket = ticket_template()  # *Call* the lambda function.
    html_file.write(filled_in_ticket)

print('fini')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文