English 中文(简体)
粉碎:需要以超文本模板取代一系列不同的次标,增加超文本或数据库成果
原标题:Python: Need to replace a series of different substrings in HTML template with additional HTML or database results

Situation:

我正在撰写一个在Avreal/mod_python的基本排时系统,该系统以主要的超文本模板的形式读取,并用额外的超文本或折中结果取代“:价值:”在整个文件中,然后将其退回用户。

我并不是试图取代所有1次降级事件。 价值可能有所不同。 有一个明确的清单,列出可接受的内容。 这不是无限制的。 数值的辛金为[上校]价值。 实例可能是“:促进:......,......:相关:......,......:结果:”。 替换可以是增加静态超文本或要求功能。 这些职能也可能有所不同。

Question:

什么是主要超文本文档中最有效率的读写方式,用适当的替换取代未知的价值观组合?


预先感谢任何想法/解决办法,

c

最佳回答

如果你能够出于任何理由利用现有的排位制度,那么你的问题似乎最好以定期表达方式解决:

import re

valre = re.compile(r :w+: )

def dosub(correspvals, correspfuns, lastditch):
  def f(value):
    v = value.group()[1:-1]
    if v in correspvals:
      return correspvals[v]
    if v in correspfuns:
      return correspfuns[v]()  # or whatever args you need
    # what if a value has neither a corresponding value to
    # substitute, NOR a function to call?  Whatever...:
    return lastditch(v)
  return f

replacer = dosub(adict, another, somefun)

thehtml = valre.sub(replacer, thehtml)

基本上,你需要两个字典(一个与相应数值相匹配的地图价值,另一个与相应职能相呼应的地图价值),一个功能要被称作是最后尝试,在任一字典中都可以看到这些价值观;上面的法典表明,如何将这些事情合在一起(如果有人使用封闭,某类人当然也这样做),以及如何将这些东西用于必要的替代任务。

问题回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签