如何在Python中编辑行(右上方和替换)

如何在Python中编辑行(右上方和替换),python,Python,我有一个文件,我需要编辑该文件中的特定行 我需要编辑这一行: "file:///c:/xxxxxxx/nameList.json#/" 进入: 姓名列表/xxxxxx/ 我该怎么做 请注意,我需要以某种方式存储“名称列表”。我正在写一个脚本,“名字列表”部分对于不同的文件是不同的 其他信息: 我只需要替换“姓名列表”前面和“姓名列表”后面的内容 但是字符串“nameList”对于不同的文件是任意的。例如,第一个文件将有nameList1,第二个文件将有nameList2…..等等。我需要为不

我有一个文件,我需要编辑该文件中的特定行

我需要编辑这一行:

"file:///c:/xxxxxxx/nameList.json#/"
进入:

姓名列表/xxxxxx/
我该怎么做

请注意,我需要以某种方式存储“名称列表”。我正在写一个脚本,“名字列表”部分对于不同的文件是不同的

其他信息:

  • 我只需要替换“姓名列表”前面和“姓名列表”后面的内容

  • 但是字符串“nameList”对于不同的文件是任意的。例如,第一个文件将有nameList1,第二个文件将有nameList2…..等等。我需要为不同的文件保留nameListX

  • regex=re.compile(“file:///c:/([^/]+)/nameList.json#/”)
    replacer=lambda g:'<{0}=“{0}://{0}.com/{0}//nameList.md”>nameList/{0}/'.格式(g.groups()[0])
    将open(“output.txt”、“w”)作为f:
    对于打开的行(“input.txt”):
    f、 写入(re.sub(正则表达式、替换符、行))
    
    也许。。。我不知道第二张照片中的xxxx和第一张照片中的xxxx有什么关系

    如果你只是想要“名字列表”的那部分

    regex = re.compile("file:///c:/[^/]+/([.]+)\.json#/")
    replacer = lambda g: '<xmlns="http://blahblah.com/sub1//{0}.md">{0} /xxxx/'.format(g.groups()[0])
    
    with open("output.txt","w") as f:
        for line in open("input.txt"):
             f.write(re.sub(regex, replacer, line))
    
    regex=re.compile(“file:///c:/[^/]+/([.]+\.json/”)
    replacer=lambda g:'{0}/xxxx/'.格式(g.groups()[0])
    将open(“output.txt”、“w”)作为f:
    对于打开的行(“input.txt”):
    f、 写入(re.sub(正则表达式、替换符、行))
    
    假设
    xxxx
    在这两种情况下都无关紧要,并且每个输入都是文件名,除了扩展名之外没有任何其他的
    ,这将得到您想要的输出:

    test = "file:///c:/xxxxxxx/nameList.json#/"
    parsed_file = test.split('.')[0].split('/')[-1]
    result = """< xxxx="xxxx://xxxx.com/xxxx//{0}.md">{0} /xxxxxx/""".format(parsed_file)
    print(result)
    
    结果是:

    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    姓名列表/xxxxxx/
    
    xxxx是否来自同一输入行?xxxx都不同。他们只是一些不知名的人物。他们不重要吗?他们不重要。我只需要替换nameList前面的内容和nameList后面的内容……但是字符串“nameList”对于不同的文件是任意的。例如,第一个文件将有nameList1,第二个文件将有nameList2…..等等,您可能需要查看。如果没有一个更明确的问题空间,就很难提供进一步的方向。我认为他试图解析出
    nameList
    ,而不是
    xxxx
    值。也许。。。我还以为不止这些。。。但从评论来看,你可能是对的……这就是为什么我先问;)虽然它实际上似乎是我们两个答案的组合。Thx的帮助:)对不起,我刚刚开始学习python。我还想知道如何才能找到“file:///c:/([^/]+)/nameList.json#/“我的文件中的这一行..我需要先找到这一行,然后再替换它..Thx!!另一个问题:我如何找到这行包含“file:///c:/xxxxxxx/nameList.json#/“在我的输入文件中?如果不知道更多细节,re:你到底想找到什么,我真的帮不了你。”。搜索、分割和做类似的事情就是模式识别。你想找到什么样的模式?您所有的输入有什么共同之处?Joran的回答可能会引导您找到正确的方法来找到类似的内容。常见的是,我想用“file:Updated answer@jduanI”开头替换所有行,非常感谢您的帮助!但是,当行中包含“file:xxx”时,替换后,它会环绕:(
    test = "file:///c:/xxxxxxx/nameList.json#/"
    parsed_file = test.split('.')[0].split('/')[-1]
    result = """< xxxx="xxxx://xxxx.com/xxxx//{0}.md">{0} /xxxxxx/""".format(parsed_file)
    print(result)
    
    test = "file:///c:/xxxxxxx/nameList.json#/"
    
    def replace_text(s):
        parsed_file = test.split('.')[0].split('/')[-1]
        return("""< xxxx="xxxx://xxxx.com/xxxx//{0}.md">{0} /xxxxxx/""".format(parsed_file))
    
    
    print(replace_text(test))
    
    with open('results.txt', 'w') as f:
        for line in open("test.txt"):
            if line.startswith('file'):
                f.write(replace_text(line) + '\n')
    
    file:///c:/xxxxxxx/nameList.json#/
    random crap
    more stuff
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    another one
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    more crap
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    
    oooo this is really getting random
    file:///c:/xxxxxxx/nameList.json#/
    file:///c:/xxxxxxx/nameList.json#/
    
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/
    < xxxx="xxxx://xxxx.com/xxxx//nameList.md">nameList /xxxxxx/