0

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Luke
  • 468
  • 1
  • 5
  • 21
  • (1) You can't insert data into a file, you have to rewrite it at least from insertion point to the end. (2) You call `find` but didn't use the position returned by it. (3) The position returned by `find` would be at beginning of `b'`. (4) If you are a beginner in Python you should start with something simpler: https://docs.python.org/3/tutorial/ – Michael Butscher Nov 22 '18 at 20:27
  • Possible duplicate of [Regular expression matching a multiline block of text](https://stackoverflow.com/questions/587345/regular-expression-matching-a-multiline-block-of-text) – KC. Nov 23 '18 at 04:44
  • You must refer to the mmaped file: mm= mmap.mmap(file.fileno(),...) . See the documentation, here is an example, too: https://docs.python.org/3/library/mmap.html – kantal Nov 23 '18 at 07:13

2 Answers2

1

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)
miike3459
  • 1,431
  • 2
  • 16
  • 32
0

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)
Luke
  • 468
  • 1
  • 5
  • 21