我如何正确地逃避这些报价?

时间:2016-02-28 15:06:02

标签: python escaping

我想为以下选择创建一个代码对象:

var app = angular.module('mainApp', []);

app.controller('register', function ($scope, $http) {

    $scope.send = function register() {
        alert("test");
        $http.post("http://52.77.16.197:8000/subscribe/", transformRequest = transformRequestAsFormPost, data = JSON.stringify({ email: $scope.email, role: $scope.role, home: $scope.home }))
            .success(function (response) {
                alert("hi");
                $scope.persons = data;
            }).error(function (response) {
                if (response.message == "already_subscribed") {
                    alert("already_subscribed");
                }
                else {
                    alert("error");
                }
            });
    }

});

   angular.module('mainApp')
    .factory("transformRequestAsFormPost",
        function () {
            function transformRequest(data, getHeaders) {
                var serializeData = function (obj, result, keyName) {
                    if (typeof (keyName) === 'undefined') {
                        keyName = null;
                    }
                    var cloneObj = JSON.parse(JSON.stringify(obj));
                    $.each(cloneObj, function (idx, v) {
                        if (v instanceof Object) {
                            var newIdx = encodeURIComponent(idx);
                            var newKeyName = keyName === null ? newIdx : keyName + "[" + newIdx + "]";
                            serializeData(v, result, newKeyName);
                        }
                        else {
                            var key = encodeURIComponent(idx);
                            if (keyName !== null) {
                                key = keyName + "[" + key + "]";
                            }
                            result.push(
                                key + "=" + encodeURIComponent(v)
                                );
                        }
                    });
                };
                var result = [];
                var headers = getHeaders();
                headers["Content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
                serializeData(data, result);
                return result.join("&");
            }
            return (transformRequest);

        });

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    //$httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";

}
]);
function register() {
    alert("submit");
    var scope = angular.element(document.getElementById("reg")).scope();
    scope.$apply(function () {
        scope.send();
    });
}

我试过了:

"LAND1"= 'Park / Recreation / Open Space' AND "LAND2"= 'Park / Recreation / Open Space'

这会出错。有没有办法保留LAND1和LAND2的双重qoutes?我如何在这方面使用格式?

2 个答案:

答案 0 :(得分:2)

以下内容如何:

>>> code_string = """"LAND1"= 'Park / Recreation / Open Space' AND "LAND2"= 'Park / Recreation / Open Space'"""
>>> code_string
'"LAND1"= \'Park / Recreation / Open Space\' AND "LAND2"= \'Park / Recreation / Open Space\''

答案 1 :(得分:0)

如果Idos的回答不是你想要的,也许这是?

>>> string = "LAND1 = 'Park / Recreation / Open Space'; LAND2 = 'Park / Recreation / Open Space'"
>>> exec(string)
>>> LAND1
'Park / Recreation / Open Space'
>>> LAND2
'Park / Recreation / Open Space'

坚持一种报价。此外,由于你是exec',因此exec'd等号的左侧不能有引号,因为分配给字符串文字不会任何意义。