Single and Double Quotes Automatic Switching
I'm having a simple problem replacing python quotes (single and double) with me automatically. Because of this, I cannot go back to the original text.
Here's an example
s1 = ('foo\'bar' , 'bar\"foo', 'dead\'\"beef', 'beef\\\'\"dead')
s2 = unicode(s1)
print repr(s2)
>>>u'("foo\'bar", \'bar"foo\', \'dead\\\'"beef\', \'beef\\\\\\\'"dead\')'
In this example, python has performed auto-swap of quote types for the first element of the tuple. This was expected, of course, since only single quotes appeared on the string. The problem I am having is I am trying to read a file that has a format exactly the same as the printed value above, including u, start quote and trailing quote. Is there a way to read the file and revert back to the original s1. In fact, I don't even need a tuple, just the lines inside. No encoding / decoding scheme I found works correctly due to auto-swap. Of course I can write a regex or a function to solve this, but there must be a python way to do this. Also etching or any other serialization is not for me.
early
source to share
Setting aside the question of quotes for a moment, let's focus on your real need:
to read a file that has a format exactly the same as the printed value above, including u, start quote and quote ... In fact, I don't even need a tuple, just lines inside
If you have a file whose content looks like this:
u'("foo\'bar", \'bar"foo\', \'dead\\\'"beef\', \'beef\\\\\\\'"dead\')'
The following program will give you access to the lines inside:
import ast
with open('x.txt') as input_file:
for line in input_file:
strings = ast.literal_eval(ast.literal_eval(line))
# You can do whatever you want with the `strings` var, e.g:
assert(strings[0] == "foo'bar")
assert(strings[0] == 'foo\'bar')
print strings[0]
Link:
source to share
It's not 100% clear to me what you want, but I wrote a script test.py
with two potential solutions, one of which is @ hitzg's:
# @hitzg solution:
s1 = ('foo\'bar', 'bar\"foo', 'dead\'\"beef', 'beef\\\'\"dead')
s2 = u', '.join([unicode(i) for i in s1])
print repr(s2)
# My tweak, in case that not quite what you want:
s1 = ("'foo\'bar'", "'bar\"foo'", "'dead\'\"beef'", "'beef\\\'\"dead'")
s2 = u', '.join([unicode(i) for i in s1])
print repr(s2)
Here is the output from this script:
In [5]: run test.py
u'foo\'bar, bar"foo, dead\'"beef, beef\\\'"dead'
u'\'foo\'bar\', \'bar"foo\', \'dead\'"beef\', \'beef\\\'"dead\''
Does any of these methods provide what you want? If not, can you explain how they differ from what you want? It might clarify things like this so that we can give you a better answer.
source to share