简体   繁体   English

python,string.replace()和\\ n

[英]python, string.replace() and \n

(Edit: the script seems to work for others here trying to help. Is it because I'm running python 2.7? I'm really at a loss...) (编辑:脚本似乎适用于其他人试图帮助。是因为我正在运行python 2.7?我真的很茫然...)

I have a raw text file of a book I am trying to tag with pages. 我有一本书的原始文本文件,我试图用页面标记。

Say the text file is: 说文本文件是:

some words on this line,
1
DOCUMENT TITLE some more words here too.
2
DOCUMENT TITLE and finally still more words.

I am trying to use python to modify the example text to read: 我试图使用python修改示例文本来阅读:

some words on this line,
</pg>
<pg n=2>some more words here too,
</pg>
<pg n=3>and finally still more words.

My strategy is to load the text file as a string. 我的策略是将文本文件加载为字符串。 Build search-for and a replace-with strings corresponding to a list of numbers. 使用与数字列表对应的字符串构建搜索和替换。 Replace all instances in string, and write to a new file. 替换string中的所有实例,并写入新文件。

Here is the code I've written: 这是我写的代码:

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 = "</p>\n<p n=%d>" % nplus
    bookstring = bookstring.replace(pgdel, htmlpg)

textout = open(output, 'w')
textout.write(bookstring)
textout.close()

print "Updates to %s printed to %s" % (input, output)

The script runs without error, but it also makes no changes whatsoever to the input text. 该脚本运行时没有错误,但它也不会对输入文本进行任何更改。 It simply reprints it character for character. 它只是为角色重印它的角色。

Does my mistake have to do with the hard return? 我的错误与艰难的回报有关吗? \\n? \\ n吗? Any help greatly appreciated. 任何帮助非常感谢。

In python, strings are immutable, and thus replace returns the replaced output instead of replacing the string in place. 在python中,字符串是不可变的,因此replace返回替换的输出而不是替换字符串。

You must do: 你必须这样做:

bookstring = bookstring.replace(pgdel, htmlpg)

You've also forgot to call the function close() . 你也忘了调用函数close() See how you have textin.close ? 看看你是如何拥有textin.close You have to call it with parentheses, like open: 你必须用括号来调用它,比如open:

textin.close()

Your code works for me, but I might just add some more tips: 您的代码适合我,但我可能只是添加一些提示:

  • Input is a built-in function, so perhaps try renaming that. 输入是一个内置函数,所以也许尝试重命名。 Although it works normally, it might not for you. 虽然它可以正常工作,但它可能不适合你。

  • When running the script, don't forget to put the .txt ending: 运行脚本时,不要忘记将.txt结尾:

    • $ python myscript.py file1.txt file2.txt
  • Make sure when testing your script to clear the contents of file2 . 确保在测试脚本时清除file2的内容

I hope these help! 我希望这些帮助!

Here's an entirely different approach that uses re (import the re module for this to work): 这是一个完全不同的方法,使用re (导入re模块使其工作):

doctitle = False
newstr = ''
page = 1

for line in bookstring.splitlines():
    res = re.match('^\\d+', line)
    if doctitle:
        newstr += '<pg n=' + str(page) + '>' + re.sub('^DOCUMENT TITLE ', '', line)
        doctitle = False
 elif res:
     doctitle = True
     page += 1
    newstr += '\n</pg>\n'
 else:
    newstr += line

print newstr

Since no one knows what's going on, it's worth a try. 由于没有人知道发生了什么,所以值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM