(编辑:这个剧本似乎对其他想帮忙的人有用。是因为我运行的是python2.7吗?我真的不知所措……)
我有一本书的原始文本文件,我试图用页面来标记。在
假设文本文件是:some words on this line,
1
DOCUMENT TITLE some more words here too.
2
DOCUMENT TITLE and finally still more words.
我尝试使用python将示例文本修改为:
^{pr2}$
我的策略是将文本文件作为字符串加载。生成与数字列表对应的字符串搜索和替换。替换字符串中的所有实例,并写入新文件。在
下面是我写的代码:from sys import argv
script, input, output = argv
textin = open(input,'r')
bookstring = textin.read()
textin.close()
pages = []
x = 1
while x<400:
pages.append(x)
x = x + 1
pagedel = "DOCUMENT TITLE"
for i in pages:
pgdel = "%d\n%s" % (i, pagedel)
nplus = i + 1
htmlpg = "
\n" % nplus
bookstring = bookstring.replace(pgdel, htmlpg)
textout = open(output, 'w')
textout.write(bookstring)
textout.close()
print "Updates to %s printed to %s" % (input, output)
脚本运行时没有错误,但它也不会对输入文本进行任何更改。它只是简单地逐字重印。在
我的错误是不是跟回报太难有关?\n?非常感谢任何帮助。在