【问题标题】:Increase number Python增加数字 Python
【发布时间】:2016-05-26 08:40:06
【问题描述】:

抱歉,我研究了大约 1 天,但我没有找到解决方案。

我有这个代码,但不能工作

number = 1
content = editor.getText()
while "printing" in content:
    content = content.replace("printing", "printing-")
    number += 1

notepad.new()
editor.addText(content)

我想用增加的数字替换文件中的 10.000 个单词。

例子

来自:

Lorem Ipsum is simply dummy text of the printing
Other example text is simply from user any
Lorem Ipsum is simply dummy text of the printing
Other example text is simply from user roger
Lorem Ipsum is simply dummy text of the printing
Other example text is simply from user milton

到:

Lorem Ipsum is simply dummy text of the printing-1
Other example text is simply from user any
Lorem Ipsum is simply dummy text of the printing-2
Other example text is simply from user roger
Lorem Ipsum is simply dummy text of the printing-3
Other example text is simply from user milton

所需的词是 printing-1, printing-2, print-3,... 到 printing-10000

我在 notepad++ 中尝试使用 python 插件但不起作用,我也知道 notepad++ 中的选项列编辑器 (alt+c) 但我无法选择 10.000 个单词。

¿如何在 notepadd++ 中使用 python 处理此任务?

感谢阅读

【问题讨论】:

  • “不工作”是什么意思?
  • content = content.replace("printing", "printing-" + str(number))
  • 问题是所有替换词是:printing-1,printing-1,printing-1...应该是printing-1,printing-2,printing-3...等我用你写的最后一个代码测试结果是一样的。

标签: python plugins notepad++


【解决方案1】:

用这个替换你的while循环:

split_content = content.split("\n")  #split content into sentences by the newline

new_content = ""

for sentence in split_content:  #split sentence into words
    for word in sentence.split(" "):
        if word == "printing":
            new_content += "printing-%s " % (number)
            number += 1
        else:
            new_content += word + " "

    new_content += "\n"  #put newlines back in to keep original formatting

你需要帮助理解代码就问!

【讨论】:

  • 您能解释一下为什么它不起作用吗?我有点不确定你的问题。
  • 嗨,马克,感谢您的支持。问题是所有替换词是:printing-1,printing-1,printing-1...应该是printing-1,printing-2,printing-3...等等。我用你写的最后一个代码测试结果是一样的。我想用增加的数字替换一个简单的文件。
  • 抱歉给你错误的代码我会努力解决的。希望很快!
  • 检查答案我用正确的代码更新了它,所以现在它应该可以工作了。如果您有任何问题,请再次询问!谢谢你给我一个很好的挑战!祝你有美好的一天,上帝保佑!
  • 如果我的解决方案有效,如果您能接受我的回答,我将不胜感激。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多