Home > OS >  Force double quotes in string representation
Force double quotes in string representation

Time:02-28

Can I replace my escape string with the repr() function or another function somehow? Repr() is using the wrong quotes for json.
I'm using this string to manually create a json file which is 3 times faster in my case.

import timeit

def escape_str(string):
    return string.replace("\\", "\\\\").replace('"', '\\"').replace("\b", "\\b").replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")

data = "hello\nworld"
print(data)
print('"'   escape_str(data)   '"', timeit.timeit('''\'"\'   escape_str(data)   \'"\'''', globals=globals(), number=1000000))
print(repr(data), timeit.timeit('''repr(data)''', globals=globals(), number=1000000))

#Output:
#hello
#world
#"hello\nworld" 0.606052448
#'hello\nworld' 0.12521575399999996

CodePudding user response:

Just use json.dumps(). It's faster than fiddling with it yourself.

Trying to make repr() output the same, I added a method escape_repr():

import timeit
import json

def escape_str(string):
    return string.replace("\\", "\\\\").replace('"', '\\"').replace("\b", "\\b").replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")

def escape_repr(string):
    return string.replace("\"","\\\"").replace("'", "\"").replace("\\x08", "\\b").replace("\\x0c", "\\f")

data = "hello \n_\"_\\_\b_\f world"
print(data)
print(json.dumps(data), timeit.timeit('''json.dumps(data)''', globals=globals(), number=1000000))
print('"'   escape_str(data)   '"', timeit.timeit('''\'"\'   escape_str(data)   \'"\'''', globals=globals(), number=1000000))
print(escape_repr(repr(data)), timeit.timeit('''escape_repr(repr(data))''', globals=globals(), number=1000000))

Results:

"hello \n_\"_\\_\b_\f world" 0.5502998
"hello \n_\"_\\_\b_\f world" 0.9209655000000001
"hello \n_\"_\\_\b_\f world" 0.6323072999999999

So json.dumps() is not only the correct way of doing it, but also the fastest.

  • Related