Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建替换文件中图案的生成器 请考虑以下列表: l = ['pfg022G', 'pfg022T', 'pfg068T', 'pfg130T', 'pfg181G', 'pfg181T', 'pfg424G', 'pfg424T']_Python_Python 3.x - Fatal编程技术网

Python 创建替换文件中图案的生成器 请考虑以下列表: l = ['pfg022G', 'pfg022T', 'pfg068T', 'pfg130T', 'pfg181G', 'pfg181T', 'pfg424G', 'pfg424T']

Python 创建替换文件中图案的生成器 请考虑以下列表: l = ['pfg022G', 'pfg022T', 'pfg068T', 'pfg130T', 'pfg181G', 'pfg181T', 'pfg424G', 'pfg424T'],python,python-3.x,Python,Python 3.x,和文件: example.conf 我想创建一个函数,它读取列表中的每个元素,查找该模式的文件,并用该列表的后续元素替换该模式。例如,列表的第一个元素是pfg022G,通读example.conf文件并搜索pfg022G,找到后替换为pdf022T。两个函数,以确保可读性。你当然可以把它们结合在一起 def replace_words(words, content): """List of words is supposed to have even ite

和文件:

example.conf


我想创建一个函数,它读取列表中的每个元素,查找该模式的文件,并用该列表的后续元素替换该模式。例如,列表的第一个元素是pfg022G,通读example.conf文件并搜索pfg022G,找到后替换为pdf022T。

两个函数,以确保可读性。你当然可以把它们结合在一起

def replace_words(words, content):
    """List of words is supposed to have even items
    Items are extracted in pairs: Find the first word
    in content and replace with next word
    """
    _iterator = iter(words)
    for _find in _iterator:
        _replace = next(_iterator)
        content = content.replace(_find, _replace)
    return content


def rewrite_file(file, words):
    """ Open the file to modify, read its content
    then apply the replace_words() function. Once
    done, write the replaced content back to the
    file. You could compact them into one single 
    function.
    """
    content = open(file, 'r').read()
    with open(file, 'w') as f:
        f.write(replace_words(words, content))



FILENAME = 'example.conf'
l = ['pfg022G', 'pfg022T', 'pfg068T', 'pfg130T', 'pfg181G', 'pfg181T', 'pfg424G', 'pfg424T']
rewrite_file(FILENAME, l)

生成器不应该对更改文件内容产生副作用?我不明白-澄清你的要求。还有,为什么你不能生成一个列表并用它创建一个生成器呢?好吧,让我们忘掉生成器吧。我只想用中的后续字符串替换文件中的模式。与文件中的模式匹配的列表仅适用于第一次运行。之后它就不再工作了请解释:您的示例文件只包含单词列表中的第一个单词。你有一个更完整的例子吗?是的,它只包含第一个单词,但一旦被替换,它将包含单词列表中的第二个单词,而一旦被替换,它将包含单词列表中的第三个单词,但情况并非如此。这应该一直持续到列表被耗尽为止。请在example.conf通过函数之后提供您希望它看起来像什么的输出。
def replace_words(words, content):
    """List of words is supposed to have even items
    Items are extracted in pairs: Find the first word
    in content and replace with next word
    """
    _iterator = iter(words)
    for _find in _iterator:
        _replace = next(_iterator)
        content = content.replace(_find, _replace)
    return content


def rewrite_file(file, words):
    """ Open the file to modify, read its content
    then apply the replace_words() function. Once
    done, write the replaced content back to the
    file. You could compact them into one single 
    function.
    """
    content = open(file, 'r').read()
    with open(file, 'w') as f:
        f.write(replace_words(words, content))



FILENAME = 'example.conf'
l = ['pfg022G', 'pfg022T', 'pfg068T', 'pfg130T', 'pfg181G', 'pfg181T', 'pfg424G', 'pfg424T']
rewrite_file(FILENAME, l)