Search code examples
pythonregexpython-3.xyamlpyyaml

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:

  - 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:

Solution

  • 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. Once the config is loaded, it's really just a nested list/dict much like a json where you can manipulate the data easily. [Here's my attempt to replicate your end result][1]:

    You can play around with the data structure, but using this should give you a lot more freedom than regex.