print string as raw string

M

Mirko Dziadzka

Hi all

I'm trying to find a way to output strings in the raw-string format, e.g.

print_as_raw_string(r"\.") should output r"\." instead of "\\."

Is there a better way than writing your own print function? Some magic
encoding?


Mirko
 
D

Diez B. Roggisch

Mirko said:
Hi all

I'm trying to find a way to output strings in the raw-string format, e.g.

print_as_raw_string(r"\.") should output r"\." instead of "\\."

Is there a better way than writing your own print function? Some magic
encoding?

There is no need to do this. Rawstrings are only different wrt to their
parsing, the resulting string is the exact same.

r"\foo" == "\\foo"
type(r"\foo") == type("\\foo")

So there is no way to write something that distinguishes them.


Can it be that you are fooled by the repr() of strings in the interpreter?
'\\foo'

This is nothing to worry about, it's just the interpreter doing an
implicit repr-call for the objects it displays.

If instead you'd do
\foo

you get what you want.


Diez
 
M

Mirko Dziadzka

Diez B. Roggisch said:
There is no need to do this. Rawstrings are only different wrt to their
parsing, the resulting string is the exact same.

I know.

Maybe a little bit more eplaination from my site. I'm going to generate
python source which contains lot of regex-strings and I want this code
to be easy readable and editable.
In fact, editing these regexes is the main reason for exporting them.

So I would prefer the more compact and readable raw-string notation
instead of the smantic equivalent string notation.

So I'm going to write these couple of lines to convert a string in this
raw-string format.

Greetings

Mirko
 
M

Mirko Dziadzka

Mirko Dziadzka said:
Hi all

I'm trying to find a way to output strings in the raw-string format, e.g.

print_as_raw_string(r"\.") should output r"\." instead of "\\."

Ok, lets make a better example:
re_list = {}
re_list['foo'] = r'\..*'
re_list['bar'] = r'.*bar.*'
print re_list
{'foo': '\\..*', 'bar': '.*bar.*'}

I would like to see a:
{'foo': r'\..*', 'bar': '.*bar.*'}


Greetings

Mirko
 
S

Steven D'Aprano

Diez said:
There is no need to do this.

How do you know? Perhaps the OP is writing a test suite where he needs to
confirm that tens of thousands of raw strings are converted correctly, and
he'd rather write a Python script to generate those raw strings than to
type them up by hand. In this case though, the OP would be better off
generating the raw strings systematically, rather than trying to go
backwards.

In any case, this is just a variation of what repr() does.
"'\\\\.'"

What the OP seems to want is something like:
"r'\.'"


I don't think that's necessarily a silly function to have, although what it
is useful for (apart from my contrived example above), I don't know.

Here is a barely tested version:

def raw_repr(s):
"""Return the repr of string s as a raw-string."""
r = repr(s)
if '\\\\' in r:
r = "r" + r.replace('\\\\', '\\')
assert not r.endswith('\\')
return r

r'\.'
 
M

Miles

...
The big issue I see is that lots of values cannot be represented
as raw strings. Your code checks for a final '\', but many "control"
characters and the quote marks are also at issue.

raw_repr('["\']a\x13\xfebc\\de') should contain several nasty cases.

It seems to me that a raw_repr should only be required to be able to
encode strings that are representable as raw strings in the first
place. Here's mine:

def raw_repr(s):
if s and s[-1] == '\\':
raise ValueError('No raw string representation; '
'string ends in backslash')
# If you want to test for control and non-ASCII characters to raise
# an exception, that's up to you
quote = None
if '\n' not in s:
if "'" not in s:
quote = "'"
elif '"' not in s:
quote = '"'
if not quote:
if "'''" not in s:
quote = "'''"
elif '"""' not in s:
quote = '"""'
if not quote:
raise ValueError('No raw string representation; '
'string contains unescapable quote characters')
return 'r' + quote + s + quote
.... print raw_repr(r'"""'"'''") # ;)
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<stdin>", line 19, in raw_repr
ValueError: No raw string representation; string contains unescapable
quote characters

-Miles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top