python 字符串替换_Python,字符串。替换()和\n
(編輯:這個劇本似乎對其他想幫忙的人有用。是因為我運行的是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?非常感謝任何幫助。在
總結
以上是生活随笔為你收集整理的python 字符串替换_Python,字符串。替换()和\n的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: boost安装_编译安装Mysql详细步
- 下一篇: 字符串反转python_Python 反