0

pagesubs()函数将文本文件读入字符串,然后使用 Pythonformat()替换 subs 参数中给出的参数。

以下是示例:
我的尝试如下:

def pagesubs(N,*subs):
    assert type(N)==str
    F= open(N,'r')
    return F.format(subs)

我收到一个错误,F is type(file)但我认为将open()文本文件读入字符串。任何帮助是极大的赞赏

编辑:示例

 pagesubs('index.html', 'December', 18, 2012)

  This will return the content of the file index.html, but 
  with "December" substituted for {0}, and 18 substituted
  for {1}, and 2012 substituted for {2}.  
4

1 回答 1

2

您需要read()在文件上调用该函数才能将其内容转换为字符串。请尝试以下代码:

def pagesubs(N, *subs):
    assert type(N)==str
    with open(N,'r') as F:
        content = F.read()
    return content.format(subs)
于 2012-11-16T04:01:43.847 回答