Trouble using python re with the format option [duplicate] - python

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 2 years ago.
I have multiple files of the format myfilexyz-200407171758.tar.gz
(myfilexyz)-(200407171758).tar.gz
Group1 is a variable.
Group2 can be of 12 to 14 digits.
Using variable substitution, I can get this working
r = re.compile('(%s)-(\d){12,13}.tar.gz' %myvar)
But if I were to try the newer format method, I get into trouble
r = re.compile('({})-(\d){12,14}.tar.gz'.format(myvar))
key '12,14' has no corresponding arguments
Obviously the {12,14} is messing up format. Is there a way around this problem and still use the format method for substitution?

From documentation,
If you need to include a bracing character in the literal text, it can be escaped by doubling:
{{ and }}.
Use
'({})-(\d){{12,14}}.tar.gz'.format(myvar)
Also, format is older way of doing it. Use f-string
f'({myvar})-(\d){{12,14}}.tar.gz'
Why not concatenate directly?
myvar + '-(\d){{12,14}}.tar.gz'

Related

Function requires a path. How do I make it into a raw path? [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!

What is actual meaning of r'^ icon in every urls? can any one explain what is his meaning and how important this are in details [duplicate]

This question already has answers here:
url.py in django, what does ^ do? [duplicate]
(2 answers)
What does the "r" in pythons re.compile(r' pattern flags') mean?
(3 answers)
Convert regular Python string to raw string
(12 answers)
What exactly is a "raw string regex" and how can you use it?
(7 answers)
Closed 1 year ago.
This is the code in question:
url(r'^dreamreals/', ListView.as_view(model = Dreamreal,
template_name = "dreamreal_list.html")),
)
url() paths are regular expressions. The ^ character anchors the regular expression to the start of the string. The r prefix to the string literal means that backslashes, etc. are not interpreted (a so-called raw string).
In modern Django, you'd probably want to use path() instead of url() (which is called re_path() these days).

Changing string to ascii in python [duplicate]

This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 3 years ago.
I need to convert word
name = 'Łódź'
to ASCII characters
output: 'Lodz'
I can't import any library like unicodedata.
I need to do it in clear python.
I've tried to encode than decode and nothing worked.
Well, a simple method would be to map and replace. This also does not require any special imports.
name = 'Łódź'
name=name.replace('Ł','L')
name=name.replace('ó','o')
name=name.replace('ź','z')
print(name)

escaping {} brackets in triple quote Python strings [duplicate]

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
how can you format a string of this form in Python 3?
'''{name}{{name}}'''.format(name="bob")
the desired output is: bob{bob}, but the above gives: bob{name}.
one solution is to add another argument to format:
'''{name1}{name2}'''.format(name1="bob", name2="{bob}")
but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?
Add one more level of {}:
'''{name}{{{name}}}'''.format(name="bob")
which outputs:
bob{bob}

Are there literal strings in Python [duplicate]

This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 5 years ago.
In F# there is something called a literal string (not string literal), basically if a string literal is preceded by # then it is interpreted as-is, without any escapes.
For example if you want to write the path of a file in Windows(for an os.walk for example) you would do it like this:
"d:\\projects\\re\\p1\\v1\\pjName\\log\\"
Or you could do this(the F# way):
#"d:\projects\re\p1\v1\pjName\log\"
The second variant looks much more clear and pleasing to the eye. Is there something of the sort in python? The documentation doesn't seem to have anything regarding that.
I am working in Python 3.6.3.
There are: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
You can use r prefix.
https://docs.python.org/2.0/ref/strings.html
TL;DR use little r
myString = r'\n'

Categories