Luke
Luke

Reputation: 467

Read a file and match multiple lines

The text file is like:

    <field>
        </field>

I want to match the block and write something in between the two field tag. I have got the following code which is from How to search for a string in text files?

!/usr/bin/env python3
import mmap
import os

with open('sample.txt', 'rb+', 0) as file, \
     mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    if s.find(b'<field>\n<\field>') != -1:
        file.write("Hello")

My solution doesn't work even if I use \t to detect the tab

'<field>\n\t<\field>'

I think my issue is how to match multiple lines that have some space or tab in it. Thanks everyone.

Upvotes: 0

Views: 762

Answers (2)

moltarze
moltarze

Reputation: 1501

Please refer to this question: Regular expression matching a multiline block of text

Your goal is simple enough using regexes. The following script finds <field> tags in the variable html, and puts <text to put between the tags> in between the tags.

import mmap
import os
import re

# do logic here

# test for what you want from variable s:

a = re.sub('<field>(.*\n*)<\/field>', '<text to put between the tags>', html)

Upvotes: 1

Luke
Luke

Reputation: 467

I got the answer from: Pythonic way of inserting lines to a file

The solution doesn't use mmap. That's true we cannot insert data into a file, but we can replace the data.

        target = "<field>Hello</field>"

        with open(os.path.join(root, filename), 'r') as file:
            filedata = file.read()

        # Replace the target string
        filedata = filedata.replace('<field></field>', target)

        # Write the file out again
        with open(os.path.join(root, filename), 'w') as file:
            file.write(filedata)

Upvotes: 0

Related Questions