python string adding backslash before single quotes

旧时模样 提交于 2019-11-27 08:49:01

问题


I have a string that contains quotes like this:

string = "\"This is my mom's vase\", said Kevin."

Problem is when I use it as a string in python it adds a backslash before single quotes, as seen here:

>>> string
'"This is my mom\'s vase", said Kevin.'
>>>

Why is this happening and what can I do about it?


回答1:


It's just escaped in the repl. If you print it out, it will show there is no slash added:

print(string)
# output: "This is my mom's vase", said Kevin.



回答2:


The Explanation

What you're seeing is the representation of your string as produced by the repr function. repr outputs strings in such a way that they're valid python literals; in other words, you can copy/paste the output of repr(string) into a python program without getting a syntax error:

>>> string
'"This is my mom\'s vase", said Kevin.'
>>> '"This is my mom\'s vase", said Kevin.'  # it's valid python code!
'"This is my mom\'s vase", said Kevin.'

Because your string contains both single quotes ' and double quotes ", python has to escape one of those quotes in order to produce a valid string literal. The same way you escaped your double quotes with backslashes:

"\"This is my mom's vase\", said Kevin."

Python instead chooses to escape the single quotes:

'"This is my mom\'s vase", said Kevin.'

Of course, both of these strings are completely identical. Those backslashes are only there for escaping purposes, they don't actually exist in the string. You can confirm this by printing the string, which outputs the string's real value:

>>> print(string)
"This is my mom's vase", said Kevin.

The Solution

There's nothing to solve! What are you still doing here? Scroll up and read the explanation again!




回答3:


I am having this same issue, I'm reading a text file which contains a html to be send as a response, when testing at postman the html gets screwed when saving it to a string variable. This only happens with double quotes ", single are returned properly, already tried everything here python: write 'backslash double-quote' string into file too

Txt File

<html>
<body>
    <table width='100%'>
        <tbody><tr class="gridgray" bgcolor="#f5f5f5">

postman GET

"<html><body><table width='100%'><tbody><tr class=\"gridgray\" bgcolor=\"#f5f5f5\">"



回答4:


I found the solution, it has nothing to do with repr(string), as @Aran-Fey mentions, it has to do with API and Jsons response.

The correct approach is that you are not returning strings or jsons dumps, but response which http protocols interprets as you mention: \" (backslaches every string).

The solution is to make a http response as follows:

from flask import request, jsonify, make_response
from flask_restful import Resource
from flask_api import status

class employeeHiring(Resource):
    def post(self):
    #YOUR CODE ....
    return make_response(jsonify({'status': 'success', 'my Dict': dict}), status.HTTP_201_CREATED)


来源:https://stackoverflow.com/questions/24052654/python-string-adding-backslash-before-single-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!