简体   繁体   English

检查文本文件并使用正则表达式查找字符串模式

[英]Examine a text file and find string patterns using regex

I am trying to loop through a text file, line by line, and search for string patterns. 我试图逐行遍历文本文件,并搜索字符串模式。 When I find the string, I want to do a find/replace, so I can make several updates in the file. 当找到字符串时,我想进行查找/替换,因此可以在文件中进行多次更新。 Here are the steps that I'm trying to get through. 这是我尝试通过的步骤。

Step 1, search for 'table_name', and grab whole line of text: 第1步,搜索“ table_name”,然后抓取整行文本:

  - class: steps.validate.Validate
    conf:
      schema_def:
        fields:
        - data_type: FLOAT
          name: Month_End_Closing_Price
        - data_type: DATETIME
          name: Month_End_Pricing_Date
    id: standardize
  - class: pipe.steps.load.Load
    end:

Okay after a lot of guess work I think this answer should be helpful, or at least point you to the right direction. 好吧,经过大量的猜测工作,我认为这个答案应该会有所帮助,或者至少可以将您指向正确的方向。

As mentioned, you want to use the yaml module to help you with this. 如前所述,您想使用yaml模块来帮助您。 Once the config is loaded, it's really just a nested list / dict much like a json where you can manipulate the data easily. 加载配置后,它实际上只是一个嵌套list / dict非常类似于json ,您可以在其中轻松地操作数据。 [Here's my attempt to replicate your end result][1]: [这是我的尝试,以复制您的最终结果] [1]:

You can play around with the data structure, but using this should give you a lot more freedom than regex. 你可以玩的数据结构,但使用这应该给您的正则表达式更多自由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM