Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2639
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <vlastimil.brom@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'subject:using': 0.04; 'function,': 0.07; "shouldn't": 0.07; '"""': 0.09; 'def': 0.13; '"="': 0.16; 'expression.': 0.16; 'guessing': 0.16; 'simplified': 0.16; 'skip:# 30': 0.16; 'subject:regex': 0.16; 'subject:file': 0.16; 'skip:r 30': 0.19; 'variable': 0.21; 'header:In-Reply-To:1': 0.22; 'e.g.': 0.22; 'values': 0.23; 'example': 0.24; 'extract': 0.25; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; 'message-id:@mail.gmail.com': 0.28; 'thanks.': 0.28; 'received:209.85.161': 0.29; 'string': 0.29; 'hi,': 0.29; 'probably': 0.30; 'pattern': 0.31; 'whitespace': 0.31; 'match': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; '...': 0.32; 'expression': 0.33; 'martin': 0.33; 'lines': 0.34; 'regular': 0.34; 'file': 0.35; 'print': 0.35; 'replacement': 0.35; 'some': 0.37; 'however': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'anything': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'split': 0.60; 'unwanted': 0.60; 'best': 0.60; 'and,': 0.63 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=CGQYZrSRM5pK0RYkww2sw3jy8P2OfRVRKQMCYk/GqQc=; b=m1z+nUjN7zM9XUbYPKPFwtEOjFZVognAsPuCLuY0/YJxmig411uVJkBdjTCuKjEF04 7b4gUr47ERw+EbsY0Vws+Jy1aAWx1z3bn6rRKAMUMJC7YBsrnx3lsIIg3U1VlNCg/dGc 0rkb2WJYq/hhyNg5n1cFaii6y90JRlxS5hPd0= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=Jz5Ba+BpkP9arbXxiezfT+nCCbxsp7fAbn6rNgZQTPi34BEsoHXMpo9oLjzunqMItj G34twOLEXZLW3mSOIrdiC2PlD3WZm6kQwZ2OgN8CIkCG5O98njrw4j+jpqDInIy0Lv/7 P55FhcOur3IlzqtMOmOZMFECVMEnA+GF606f0= |
| MIME-Version | 1.0 |
| In-Reply-To | <c0082545-84a6-4bc5-b734-1c0f89ae27cd@r4g2000prm.googlegroups.com> |
| References | <c0082545-84a6-4bc5-b734-1c0f89ae27cd@r4g2000prm.googlegroups.com> |
| Date | Tue, 5 Apr 2011 10:32:11 +0200 |
| Subject | Re: replace regex in file using a dictionary |
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.38.1301992333.9059.python-list@python.org> (permalink) |
| Lines | 51 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1301992333 news.xs4all.nl 81476 [::ffff:82.94.164.166]:52254 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:2639 |
Show key headers only | View raw
2011/4/5 Martin De Kauwe <mdekauwe@gmail.com>:
> Hi,
>
> So i want to replace multiple lines in a text file and I have reasoned
> the best way to do this is with a dictionary. I have simplified my
> example and broadly I get what I want however I am now printing my
> replacement string and part of the original expression. I am guessing
> that I will need to use a more complicated expression than I am
> currently am to achieve what I want?
>
> my aim is to match the expression before the "=" and replace the
> string after the equals...
>
> My failed example...
> [...]
>
> thanks.
> --
>
Hi,
I guess, you would have to split the line and extract the substrings
before and after "=" and replace only the value part.
Or you can use regular expression replace with a replacement function,
e.g. like the following sample.
The re pattern would probably need some tweaking to match the variable
names, the values and the whitespace around "="; and, of course, it
shouldn't match anything unwanted in the original text ...
hth,
vbr
####################################
import re
orig_str = """structcn = 150.0
metfrac0 = 0.85
unknown7 = 38.2
"""
replacement_dict = {"structcn": "999.0", "metfrac0": "0.85"}
def repl_fn(m):
return m.group(1) + m.group(2) + replacement_dict.get(m.group(1),
m.group(3))
new_str = re.sub(r"([\w\d_]+?)( = )([\d.-]+)", repl_fn, orig_str)
print new_str
####################################
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
replace regex in file using a dictionary Martin De Kauwe <mdekauwe@gmail.com> - 2011-04-05 00:59 -0700
Re: replace regex in file using a dictionary Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-04-05 10:32 +0200
Re: replace regex in file using a dictionary nn <pruebauno@latinmail.com> - 2011-04-05 08:41 -0700
Re: replace regex in file using a dictionary Martin De Kauwe <mdekauwe@gmail.com> - 2011-04-05 16:12 -0700
csiph-web