Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the "lines" your file is divided into are of reasonable lengths, and there are no binary sequences in it that "reading as text" would break, you can use <code>fileinput</code>'s handy "make believe I'm rewriting a file in place" functionality:</p> <pre><code> import re import fileinput tagre = re.compile(r"&lt;o:p&gt;.*?&lt;/o:p&gt;") def sub(mo): return mo.group().replace(r"'", r"\'") for line in fileinput.input('thefilename', inplace=True): print tagre.sub(sub, line), </code></pre> <p>If not, you'll have to simulate the "in-place rewriting" yourself, e.g. (oversimplified...):</p> <pre><code> with open('thefilename', 'rb') as inf: with open('fixed', 'wb') as ouf: while True: b = inf.read(1024*1024) if not b: break ouf.write(tagre.sub(sub, b)) </code></pre> <p>and then move <code>'fixed'</code> to take place of <code>'thefilename'</code> (either in code, or manually) if you need that filename to remain after the fixing.</p> <p>This is oversimplified because one of the crucial <code>&lt;o:p&gt; ... &lt;/o:p&gt;</code> parts might end up getting split between two successive megabyte "blocks" and therefore not identified (in the first example, I'm assuming each such part is always fully contained within a "line" -- if that's not the case then you should not use that code, but the following, anyway). Fixing this requires, alas, more complicated code...:</p> <pre><code> with open('thefilename', 'rb') as inf: with open('fixed', 'wb') as ouf: while True: b = getblock(inf) if not b: break ouf.write(tagre.sub(sub, b)) </code></pre> <p>with e.g.</p> <pre><code> partsofastartag = '&lt;', '&lt;o', '&lt;o:', '&lt;o:p' def getblock(inf): b = '' while True: newb = inf.read(1024 * 1024) if not newb: return b b += newb if any(b.endswith(p) for p in partsofastartag): continue if b.count('&lt;o:p&gt;') != b.count('&lt;/o:p&gt;'): continue return b </code></pre> <p>As you see, this is pretty delicate code, and therefore, what with it being untested, I can't <em>know</em> that it is correct for your problem. In particular, can there be cases of <code>&lt;o:p&gt;</code> that are NOT matched by a closing <code>&lt;/o:p&gt;</code> or vice versa? If so, then a call to <code>getblock</code> could end up returning the whole file in quite a costly way, and even the RE matching and substitution might backfire (the latter would also occur if SOME of the single-quotes in such tags are already properly escaped, but not all).</p> <p>If you have at least a GB or so, avoiding the delicate issues with block division, at least, IS feasible, since everything should fit in memory, making the code much simpler:</p> <pre><code> with open('thefilename', 'rb') as inf: with open('fixed', 'wb') as ouf: b = inf.read() ouf.write(tagre.sub(sub, b)) </code></pre> <p>However, the other issues mentioned above (possible unbalanced opening/closing tags, etc) might remain -- only you can study your existing defective data and see if it affords such a reasonably simple approach at fixing!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload