首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >增加数字Python

增加数字Python
EN

Stack Overflow用户
提问于 2016-02-15 20:24:48
回答 1查看 115关注 0票数 1

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

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

代码语言:javascript
复制
number = 1
content = editor.getText()
while "printing" in content:
    content = content.replace("printing", "printing-")
    number += 1

notepad.new()
editor.addText(content)

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

示例

发自:

代码语言:javascript
复制
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

至:

代码语言:javascript
复制
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

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

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

感谢您的阅读

EN

回答 1

Stack Overflow用户

发布于 2016-02-15 20:43:10

用下面的代码替换您的while循环:

代码语言:javascript
复制
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

如果你需要帮助理解代码,尽管问吧!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35409333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档