【问题标题】:Regex string substitution in file文件中的正则表达式字符串替换
【发布时间】:2021-10-10 05:35:47
【问题描述】:

我需要替换文件中的字符串。但暂时没有成功。欢迎任何建议。

我有包含以下内容的文件 output.txt:

2021-07-28 10:27:49,869 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT <xml code following></xml code following> 
2021-07-28 10:27:49,881 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT <xml code following></xml code following> 
2021-07-28 10:27:51,834 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT <xml code following></xml code following> 
2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT <xml code following></xml code following> 

我有代码,用于制作每一行的第一个par:

2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT

拥有:

<time>2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time>

代码如下:

regex_time_xml_div = r"\d+-\d+-\d+ \d+:\d+:\d+,\d+\s[0-z]{7}\s[0-z]{9}\s.{34}"
            with open(r'output\output.txt',"r+") as file:
                list_of_timestamps = []
                for line in file:
                    if re.search(regex_time_xml_div, str(line)):
                        list_of_timestamps.append(line)
                content = file.read()
                for i in list_of_timestamps:
                    result = re.sub(regex_time_xml_div,'<time>'+i+'</time>',content)
                    print(result,file=open(r'output\output_new.txt',"a"))

但结果文件 output_new.txt 有 4 个空行。任何人都可以请支持这一点。谢谢你的建议。


感谢 4Fingers,我已将代码更改为:

            regex_time_xml_div_1 = r"^"
            regex_time_xml_div_2 = r"\s<"
            xml_time_1 = '<time>'
            xml_time_2 = '</time><'
            with open(r'output\output.txt',"r+") as file:
                for line in file:
                    xml_time = re.sub(regex_time_xml_div_1, xml_time_1, line)      
                    print(xml_time,file=open(r'output\beg_line.txt',"a")) 
            
            with open(r'output\new.txt',"r+") as file:
                for line in file:
                    xml_time = re.sub(regex_time_xml_div_2, xml_time_2, line)
                    print(xml_time,file=open(r'output\after_time.txt',"a"))

就是这样,现在输出看起来像预期的那样。但是额外文件的数量看起来有点令人困惑。用 os.remove 删除了这些

【问题讨论】:

  • 可能与您的正则表达式的最后一部分有关,在这里您匹配所有内容。为了保存,我将添加行尾和行首:$ 和 ^.
  • @4Fingers 有道理,我现在就测试。谢谢
  • 成功了,非常感谢。现在唯一令人不安的是创建的文件数量。
  • 很高兴听到,您可以使用正则表达式 ^[a-z0-9\,\-\:\/\s\.]+\&lt; 一次性捕获所有案例,不要忘记使用不区分大小写的标志,或添加 A-Z。在这里,您假设您尝试匹配的部分中没有出现

标签: python-3.x regex logging substitution


【解决方案1】:

所以基本上答案是:

            regex_time_xml_div_1 = r"^"
            regex_time_xml_div_2 = r"\s<"
            xml_time_1 = '<time>'
            xml_time_2 = '</time><'
            with open(r'output\output.txt',"r+") as file:
                for line in file:
                    xml_time = re.sub(regex_time_xml_div_1, xml_time_1, line)      
                    print(xml_time,file=open(r'output\beg_line.txt',"a")) 
            
            with open(r'output\new.txt',"r+") as file:
                for line in file:
                    xml_time = re.sub(regex_time_xml_div_2, xml_time_2, line)
                    print(xml_time,file=open(r'output\after_time.txt',"a"))

非常感谢您的帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多