Python Forum
changing { and } in str.format()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing { and } in str.format()
#1
i have a bunch of generated strings (read from a file) that are being processed in my script by str.format() with a bunch of args. today, i ran into a case where i have a '{' in one of the strings and str.format() is upset by it. the normal solution with str.format is '{'->'{{'. but this is turning out to be impossible to do because all the '{' are being doubled. what i can do, easily, is have the various '{}' and '{foo}' that are being added changed to something else like maybe '[]' and '[foo]' (i will need to grep this data a few times to see what i can safely pick).

is there a way to have str.format() use an alternate for '{}' and '{foo}'?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
could you please rephrase the question? Possibly including practical examples of what you mean instead of describing them? I would be really happy to help but I just can't understand it...
Reply
#3
Could you post an example
Reply
#4
I suggest to use mako templates which need a $ before the {}
>>> from mako.templates import Template
>>> s = "{et{ev}t}d${foo}}tu^tt${bar}tx}"
>>> Template(s).render(foo='hello', bar='world')
'{et{ev}t}dhello}tu^ttworldtx}'
>>> s = "{et{ev}t}d${foo}}tu^tt${bar**3}tx}"
>>> Template(s).render(foo='hello', bar=8)
'{et{ev}t}dhello}tu^tt512tx}'
Of course, there are still issues if the strings contain many % because this is also a part of mako's syntax.
Reply
#5
Here is another solution: a subclass of str that allows one to use !{ and !} for format syntax instead of { and }.
import re
exclam = 'exclam'
_trans = {'!{': '{', '!}': '}', '{': '{{', '}': '}}'}
_motif = re.compile(r'\!\{|\!\}|\{|\}')
def _sub_motif(match):
    return _trans[match.group()]

class exclamstr(str):
    """Strings that use !{ and !} as format() method's delimiters."""
    def format(self, *args, **kwargs):
        kwargs.setdefault('exclam', '!')
        return _motif.sub(
            _sub_motif, self).format(*args, **kwargs)
    
    
if __name__ == '__main__':
    s = 'uedevu_{tvy€€€€}\!{foo!}!{exclam!}{hi}  rrtet}{{{ a!{bar!}}}'
    print(exclamstr(s).format(foo=' HELLO ', bar=' WORLD '))
Output:
uedevu_{tvy€€€€}\ HELLO !{hi} rrtet}{{{ a WORLD }}
If you actually want to write !{ in the output string, use !{exclam!}{
Reply
#6
(May-11-2019, 05:15 PM)Gribouillis Wrote: If you actually want to write !{ in the output string, use !{exclam!}{

i assume where you say "write" you mean the result of str.format() since a very common use of str.format() is in a print() call.

i actually want '{' in the result. the first case i ran into was a line from a bash script which had '{' and the matching '}' to define a bash function.

a rough approximation of relevant parts of some code is below. the variable named "vard" is a big dictionary of settings.

    bscript = []
    ...
    for line in scriptfile:
        ...
        bscript.append(line.format(**vard))
    ...
    bashscript = '\n'.join(bscript+[''])
then one of the lines it gets from the file is:

Output:
upload(){ aws s3 sync /tmp/data s3://{bucket}/{prefix};}
if i had control over the incoming data i would use '{{' instead of the first '{', and '}}' instead of the last '}'. but replacing the '{' and '}' correctly turns out to be very hard. but i can replace the '{' and '}' in substitutions when the file is generated. what i am hoping for is some way to tell str.format to look for '[' and ']' instead, such as str.format(_alt='[]').

this is the first case have with '{' being used in the bash script being put in the instance. everything before did not have '{' or '}' and the subs worked OK.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Quote:what i am hoping for is some way to tell str.format to look for '[' and ']' instead, such as str.format(_alt='[]').
I don't understand. If you don't have control on the input, how can you insert the '[' and ']'? Also if you can do this, you could as well insert '!{' and '!}' and use my method.
Reply
#8
the configuration of the generator lets me insert the text i want for certain data items. i have given it strings like "{bucket}" for the S3 bucket name. it seems like it was not expecting anyone to do dynamic post-generation processing. i may have to figure out a way to dynamically build its config and run it for each instance batch (in each AWS region).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
i did some string juggling to work around this. i configured the generator of the files to use some characters in place of '{' and'}' as if str.format() used them. that avoids different things with '{' or '}' from getting confused. then when i read in the file i swap characters around so '{' and'}' are present only where the format substitutions are (e.g. "{bucket}") and i call its .format() method with the settings. then i swap the characters back so the '{' and'}' are back where they are needed in the bash code being generated.

it turns out my original idea of using '[' and ']' would have similar problems because they are also used by the generated bash code. i found that '%' and '^' are not used, so those are the characters i am swapping with ... "{}" <-> "%^". so i read in "%bucket^", change it to "{bucket}" while the bash '{' and '}' get changed to '%' and '^', call .format(), then change '%' and '^' back to '{' and '}'. done. since this is being done with Python str type i could use some high up characters such as chr(65536) and chr(65537) which are extremely unlikely to be in the generated bash code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
Quote: i could use some high up characters such as chr(65536) and chr(65537) which are extremely unlikely to be in the generated bash code.
You could also find dynamically two unicode characters that are not in the bash code. It is even less likely that the bash code uses all the unicode characters.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,669 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  changing format to Int Scott 3 2,466 Jun-19-2019, 06:33 AM
Last Post: ODIS
  Changing Number Format moby 4 5,058 May-24-2019, 11:04 PM
Last Post: snippsat
  Date Format Changing Program Not Working pyth0nus3r 2 4,730 Jan-28-2017, 10:34 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020