小编Chr*_*row的帖子

阻止xml.etree.ElementTree.xml()在Element标记中包含网站名称

我正在使用python并尝试获取一些XML并将其转换为dict.代码工作正常,除了一些奇怪的文本被添加到元素标签,然后被添加到dict属性名称.这个文本似乎是"WebServiceGeocodeQueryResult"属性的值:"xmlns".

我的代码看起来像这样:

import xml.etree.ElementTree as ET
import xml_to_dictionary # This is some code I found, it seems to work fine:
                         # http://code.activestate.com/recipes/410469-xml-as-dictionary/

def doSomeStuff()
    theXML = """
<?xml version="1.0" encoding="utf-8"?>
    <WebServiceGeocodeQueryResult 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns="https://webgis.usc.edu/">

        <TransactionId>7307e84c-d0c8-4aa8-9b83-8ab4515db9cb</TransactionId>
        <Latitude>38.8092475915888</Latitude>
        <Longitude>-77.2378689948621</Longitude>
        ...
"""

    tree = ET.XML(result.content)   # this is where the element names get the added '{https://webgis.usc.edu/}'
    xmldict = xml_to_dictionary.XmlDictConfig(tree)
Run Code Online (Sandbox Code Playgroud)

正如您在调试器中看到的那样,对象"tree"中的元素名称具有恼人的前缀: "{ https://webgis.usc.edu/ }": 在此输入图像描述

并且此前缀被转换为dict属性名称: 在此输入图像描述

python xml

3
推荐指数
1
解决办法
312
查看次数

app引擎会自动缓存频繁查询吗?

我似乎记得在某处读取谷歌应用程序引擎自动将非常频繁的查询结果缓存到内存中,以便更快地检索它们.

它是否正确?

如果是这样,这些查询的数据存储读取是否仍有费用?

python google-app-engine memcached bigtable

3
推荐指数
1
解决办法
1258
查看次数

从序列超出范围的字符串中删除所有字符

删除范围之外的所有字符的好方法是什么:ordinal(128)来自python中的字符串?

我在python 2.7中使用hashlib.sha256.我得到了例外:

UnicodeEncodeError:'ascii'编解码器无法对位置13中的字符u'\ u200e'进行编码:序数不在范围内(128)

我认为这意味着一些时髦的角色进入了我想要散列的字符串.

谢谢!

python regex ascii ordinal hashlib

3
推荐指数
1
解决办法
7797
查看次数

无法推送到转移的存储库:"远程端意外挂断"

我将存储库的所有权转让给了我拥有所有权的组织.但是,现在我无法推送到存储库.我收到此错误:

$ git push origin master
ERROR: Repository not found.
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)

为了能够推动再次推送,我需要采取哪些步骤.

git github repository

3
推荐指数
1
解决办法
3795
查看次数

以HTML格式显示表格数据

我以前总是使用表格.但是,我被告知在现代HTML设计中使用表格是一个很大的禁忌.在现代HTML中显示表格数据有哪些好的和"可接受的"方法?

我想做这样的事情:

First_Name   Last_Name    Phone Number
Britney      Spears       555-555-5555
Jon          Bon Jovi     444-444-4444

我不是100%清楚为什么我不应该为此使用表格.但我见过的人使用<li>的,他们使用CSS,使操作起来更象<div>

使用HTML和CSS设置这样的数据的好方法是什么?

html css html-lists tabular

3
推荐指数
2
解决办法
3661
查看次数

防止"jQuery(html)"触发浏览器请求图像和其他引用的内容

使用jQuery从文本创建新的DOM元素.

例:

jQuery('<div><img src="/some_image.gif"></img></div>');
Run Code Online (Sandbox Code Playgroud)

执行此语句时,它会使浏览器从服务器请求文件'some_img.gif'.

有没有办法执行此语句,以便可以从DOM遍历中使用生成的jQuery对象,但实际上不会导致浏览器使用图像和其他引用内容的请求命中服务器?

例:

var jquery_elememnts = jQuery('<div><img class="a_class" src="/some_image.gif"></img></div>');
var img_class = jquery_elememnts.find('img').attr('class');
Run Code Online (Sandbox Code Playgroud)

我现在唯一的想法是使用正则表达式从图像元素中删除所有'src'标签以及在使用jQuery评估HTML之前触发浏览器请求的其他内容.

如何使用jQuery来评估HTML,而不会触发浏览器向服务器请求评估HTML中的引用内容?

谢谢!

javascript jquery dom

3
推荐指数
1
解决办法
510
查看次数

Python应用程序中是否需要"cgi.escape"?

正在查看开发人员的代码.他在Python应用程序中做过我以前从未见过的事情.他的背景是PHP,只是学习python,所以我不知道这是否可能是他习惯使用的不同系统架构的延续.

他告诉我,此代码的目的是防止用户通过代码插入攻击应用程序.我很确定这对我们的用例来说是不必要的,因为我们从不将数据作为代码进行评估,但我只是想确认并询问社区.

# Import library
from cgi import escape

# Get information that the client submitted
fname   = GET_request.get('fname',   [''] )[0]

# Make sure client did not submit malicious code <- IS THIS NECESSARY?
if fname:
    fname = escape(fname)
Run Code Online (Sandbox Code Playgroud)
  • 这在Python应用程序中通常是必需的吗?
  • 在什么情况下有必要?
  • 在什么情况下没有必要?

python cgi code-injection

3
推荐指数
1
解决办法
4144
查看次数

检查Python中dict对象中是否存在属性集合

检查Python中dict对象中是否存在属性集合的好方法是什么?

目前我们正在这样做,但似乎可能有更好的方法:

properties_to_check_for = ['name', 'date', 'birth']
for property in properties_to_check_for:
    if property not in dict_obj or dict_obj[property] is None:
        return False
Run Code Online (Sandbox Code Playgroud)

非常感谢!

python dictionary

3
推荐指数
1
解决办法
374
查看次数

设置内容/媒体类型 - Restlet

如何在Restlet(谷歌应用引擎版本2.0)中设置内容类型?在这种情况下,我想将内容类型设置为""text/xml".

我有:

public class SubResource  extends ServerResource {

 @Get
 public Representation get(Representation representation){

    setStatus(Status.SUCCESS_OK);
    StringRepresentation sr = new StringRepresentation(getSomeXml());

    return sr;
 }
}
Run Code Online (Sandbox Code Playgroud)

即使它是在Representation中设置的值,或者它是从ServerResource类设置的方式与返回代码相同,我也不确定.

回答:

    StringRepresentation sr = new StringRepresentation(getSomeXml());
    sr.setMediaType(MediaType.TEXT_XML);
Run Code Online (Sandbox Code Playgroud)

java content-type restlet

2
推荐指数
2
解决办法
7165
查看次数

在restlet中设置cookie

我有一点时间在restlet中设置一个cookie,这是我到目前为止所拥有的::

public class CookieTestResource extends ServerResource {

    @Post
    public Representation post(Representation representation){
        CookieSetting cS = new CookieSetting(
                1, 
                "cookieName", 
                "cookieValue"
                );
        Series<CookieSetting> cookies = new Series<CookieSetting>(); //<--PROBLEM
        cookies.add(cS);
        this.setCookieSettings(cookies);
        // SEND RESPONSE
        setStatus(Status.SUCCESS_OK);
        return new StringRepresentation("");
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是我无法实例化一个类型为"org.restlet.util.Series"的类,我找不到任何可以实例化的子类.这似乎是一个愚蠢的问题.但我不知道该怎么做.另外,我似乎经常使用Restlet来解决这个问题.通常我无法弄清楚如何使用API​​中的这个工具,当我搜索示例时,我找不到.还有其他方法我应该引用Restlets的文档吗?

cookies restlet

2
推荐指数
1
解决办法
3435
查看次数