File python 3.3在500个文件中搜索70个单词并替换它们
我会回答任何我能回答的问题 基本上,我有一个70个单词的列表,我在500多个文件中查找,我需要用新单词和数字替换它们 即。。。查找“hello”并替换为“hello 233.4”,但替换为70个单词/数字和500多个文件 我在这里找到了一篇信息丰富的帖子,但我一直在阅读有关sys.argv、re、搜索、替换等的文章。。等等我不能理解这段代码。我一直在用scriptname.py“-I”和“-o”从Windows7的“cmd”窗口“调用”(我想)它 如果有人能将示例输入搜索列表路径“c:/input/file/path/searchlist.txt”和待搜索文件的示例路径“c:/search/this/file/searchme.txt”放在正确的位置,请!(我会自己尝试让它在文件夹中的每个文件中重复,并自己高亮显示或加粗替换内容。) 我试过很多组合。。。我可以检查我所做的每一个修改,并且可以输入天/页/天/页。。。每一天/每一页都变得越来越愚蠢 谢谢。。。或者,如果您知道另一种方式,请提出建议 以下是原始帖子的链接:File python 3.3在500个文件中搜索70个单词并替换它们,file,search,python-3.x,replace,directory,File,Search,Python 3.x,Replace,Directory,我会回答任何我能回答的问题 基本上,我有一个70个单词的列表,我在500多个文件中查找,我需要用新单词和数字替换它们 即。。。查找“hello”并替换为“hello 233.4”,但替换为70个单词/数字和500多个文件 我在这里找到了一篇信息丰富的帖子,但我一直在阅读有关sys.argv、re、搜索、替换等的文章。。等等我不能理解这段代码。我一直在用scriptname.py“-I”和“-o”从Windows7的“cmd”窗口“调用”(我想)它 如果有人能将示例输入搜索列表路径“c:/inpu
import re
import sys
def main():
if len(sys.argv) != 3:
print("Usage: %s fileofstufftofind filetofinditin" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1]) as f:
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in f]
there = re.compile('|'.join(patterns))
with open(sys.argv[2]) as f:
for i, s in enumerate(f):
if there.search(s):
print("Line %s: %r" % (i, s))
main()
以下是原始帖子的代码:
import re
import sys
def main():
if len(sys.argv) != 3:
print("Usage: %s fileofstufftofind filetofinditin" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1]) as f:
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in f]
there = re.compile('|'.join(patterns))
with open(sys.argv[2]) as f:
for i, s in enumerate(f):
if there.search(s):
print("Line %s: %r" % (i, s))
main()
也许你可以试试这个。。。
将所有500个文件放在同一个目录中,然后从那里开始处理。您上面发布的代码对于您的作业来说可能太复杂了。也许以下更简单的内容更容易理解:
# example variables
word_mapping = [['horse', 'donkey'], ['left', 'right']]
filename = 'C:/search/this/file/searchme.txt'
# load the text from the file with 'r' for "reading"
file = open(filename, 'r')
text = file.read()
file.close()
# replace words in the text
for find_word, replacement in word_mapping:
text = text.replace(find_word, replacement)
# save the modified text to the file, 'w' for "writing"
file = open(filename, 'w')
file.write(text)
file.close()
要加载要替换的单词列表,只需执行以下操作:
words_path = 'C:/input/file/path/searchlist.txt'
with open(words_path) as f:
word_mapping = [line.split() for line in f]
str.split()。例如,如果您有一个逗号分隔的文件,您可以使用line.split(',')
,并在逗号处拆分
作为对您在上面发布的代码的解释。。发生了两件不同的事情,所以让我们把它分成几部分
if len(sys.argv) != 3:
print("Usage: %s fileofstufftofind filetofinditin" % sys.argv[0])
sys.exit(1)
此特定脚本将wordslist和目标文件的路径作为命令行参数接收,因此您可以将此脚本作为python script\u name.py wordslist\u file target\u file
运行。换句话说,您不需要在脚本中硬编码文件路径,而是让用户在运行时提供它们
代码的第一部分通过检查sys.argv
的长度来检查已传递给脚本的命令行参数的数量,这是一个包含命令行参数作为字符串的列表。当命令行参数的数量不等于3时,将打印错误消息。第一个(或第零个)参数是脚本的文件名,因此将sys.argv[0]
作为错误消息的一部分打印出来
with open(sys.argv[1]) as f:
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in f]
there = re.compile('|'.join(patterns))
这将打开一个包含单词的文件(文件名等于sys.argv[1]
),并为它们编译正则表达式对象。正则表达式可以让您更好地控制匹配的单词,但它有自己的“迷你语言”,如果您没有使用它的经验,它可能会让您感到非常困惑。请注意,此脚本只查找单词而不替换它们,因此使用单词的文件每行仅包含一个“单词”
with open(sys.argv[2]) as f:
for i, s in enumerate(f):
if there.search(s):
print("Line %s: %r" % (i, s))
这将打开目标文件(第二个命令行参数sys.argv[2]
中的文件名),并在该文件中的行上循环。如果一行包含单词列表中的一个单词,则将打印整行。感谢您提供的链接!这肯定有助于“浏览”文件夹。我仍然需要找出如何根据我的列表搜索它们。脚本工作正常。我仍然在尝试找出原始代码…这样我就可以在我的单词列表中使用一个文件。谢谢你的解决方案,如果你对第一部分代码有任何澄清,请?@user2779846,我已经添加了一些额外的解释这就是你想知道的吗?很好的解释。我一定会发布我的最终代码。这很有意义。