[Note] RepresenterError

What I wanted to do -Open a specific http.conf and get the port number with a regular expression -Store the acquired number in a variable and spit it out with yaml

But the following error occurred

Function caller

vh_https_port = self.func_extract_port("^<VirtualHost \*:", ">", vh_port_line_https)
*vh_port_line_https=A string containing the port number

Processing to extract the actual port


    def func_extract_port(self, pattern_s, pattern_e, string):
        try:
            #Exclude commented out lines
            regex = r'(\d{1,5})'
            r = re.compile(pattern_s+regex+pattern_e)
            result_word = r.search(string)
        except:
            print(string+"Against"+pattern_s+"When"+ pattern_e +"I searched for but not found")
            result_word = ""

        return result_word

error

yaml.representer.RepresenterError: cannot represent an object: <_sre.SRE_Match object; span=(0, 21), match='<VirtualHost *:1111>'>

Cause

The result obtained by search is not a string that is a match object, so even if it can be stored in a variable, I was angry that it could not be represented.

Correspondence

result_word = r.search(string).group(1) And made the return value of the function a string

Recommended Posts

[Note] RepresenterError
Note
Note
Note
Django note 4
pyenv note
GroupBy Note
argparse note
Django Note 5
Note: Python
Ansible Note
Python note
Django Note 1
direnv note
Django note 3
Django note 2
[Note] Image resizing
Python study note_002
Note: Python Decorator
Python programming note
[Python] Learning Note 1
Kinesis Firehose Note
Python study note_004
[Note] Regarding Tensorflow
Note: confusing nonlocal
PyData.Osaka Meetup # 2 Note
Python study note_003
Completely personal note
Flask's personal note # 2
TensFlow Preferences Note
[Note] openCV + python
Note about awk
Just a note
PyCharm Preferences Note
Note: List comprehension
Python beginner's note
Flask's personal note # 1
Jupyter Study Note_003
Jupyter Study Note_007
[Note] pandas unstack
Jupyter Study Note_005