小编use*_*278的帖子

Python 2.7 读取模板并返回带有替换的新文件

我目前正在将我的数据加载到一个变量中(如下所示为“数据”),然后读取我的模板文件并用“数据”中包含的变量替换 %s。这是我的页面读取、替换、写入然后在本地服务器代码上显示新页面:

def main
    contents = makePage('varibletest.html', (data['Address'], data['Admin'], data['City'], data['ContractNo'], data['DealStatus'], data['Dealer'], data['Finance'], data['FinanceNumber'], data['First'], data['Last'], data['Message'], data['Notes'], data['Result'], data['SoldDate'], data['State'], data['Zip']))   # process input into a page
    browseLocal(contents, 'Z:/xampp/htdocs/', 'SmartFormTest{}.php'.format((data['ContractNo']))) # display page

def fileToStr(fileName): 
    """Return a string containing the contents of the named file."""
    fin = open(fileName); 
    contents = fin.read();  
    fin.close() 
    return contents

def makePage(templateFileName, substitutions):
 """Returns a string with substitutions into a format string taken
 from the named file.  The single parameter substitutions must be in …
Run Code Online (Sandbox Code Playgroud)

python string variable-declaration python-2.7

2
推荐指数
1
解决办法
3279
查看次数

标签 统计

python ×1

python-2.7 ×1

string ×1

variable-declaration ×1