相关疑难解决方法(0)

星号*在Python中意味着什么?

*在C中具有特殊含义,就像在C中一样吗?我在Python Cookbook中看到了这样的函数:

def get(self, *a, **kw)
Run Code Online (Sandbox Code Playgroud)

您能否向我解释或指出我能在哪里找到答案(Google将*解释为外卡字符,因此我找不到满意的答案).

非常感谢你.

python

295
推荐指数
5
解决办法
17万
查看次数

在python3中将字典的键和值从`bytes`转换为`str`的​​最快方法

这里是python2的优秀示例.我对python3的翻译(如下所示)适用于我的所有测试.

def convert(data):
    if isinstance(data, bytes):
        return data.decode('ascii')
    elif isinstance(data, dict):
        return dict(map(convert, data.items()))
    elif isinstance(data, tuple):
        return map(convert, data)
    else:
        return data
Run Code Online (Sandbox Code Playgroud)

看起来就像我对从py2移植到py3的库大量使用它.

有没有人有更好的设计来完成同样的任务?这个可以优化吗?

python if-statement

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

python TypeError:必须是没有NULL字节的编码字符串,而不是str

试图熟悉python的标准库,并在我的Windows机器上做一些乱码.使用python 2.7我有以下小脚本,用于查看目录并在从文件名中删除数字后重命名其中的所有文件.我得到一个类型错误,上面写着"必须是没有NULL字节的编码字符串,而不是str"

它调用了下面提到的第5行和第18行,其中我使用了os.path.exists.

任何帮助将不胜感激!

    import os, re, string, glob

    path = os.path.normpath('C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest')

ln5:if os.path.exists(path):
        print "path exists at " + path
        for file in glob.glob(os.path.join(path, '*.jpg')):
            new_path = os.path.join(os.path.dirname(file), re.sub('\d', '', os.path.basename(file)))
line18:     if not os.path.exists(new_path):
                os.rename(file, new_path)
Run Code Online (Sandbox Code Playgroud)

windows scripting python-2.7

8
推荐指数
2
解决办法
4万
查看次数

编码从unicode到ascii的列表中的字典键

我有来自facebook的朋友列表的示例回复:

[{u'uid': 513351886, u'name': u'Mohammed Hossein', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs643.snc3/27383_513351886_4933_t.jpg'},
    {u'uid': 516583220, u'name': u'Sim Salabim', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs348.snc4/41505_516583220_5681339_t.jpg'}]
Run Code Online (Sandbox Code Playgroud)

我如何解析这个列表编码字典的关键字到ascii?我尝试过这样的事情:

response = simplejson.load(urllib.urlopen(REST_SERVER, data))
for k in response:
    for id, stuff in k.items():
        id.encode("ascii")
        logging.debug("id: %s" % id)
return response
Run Code Online (Sandbox Code Playgroud)

但是编码的密钥没有保存,因此我仍然得到unicode值.

python django unicode encoding dictionary

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

如何将嵌套字典的所有值转换为字符串?

我正在编写一个 python 应用程序,其中有一个可以嵌套到任何级别的变量字典。

任何级别的键都可以是 int 或 string。但我想将所有级别的所有键和值都转换为字符串。字典的嵌套方式是可变的,这使得它有点复杂。

{
    "col1": {
        "0": 0,
        "1": 8,
        "2": {
            0: 2,
        }
        "3": 4,
        "4": 5
    },
    "col2": {
        "0": "na",
        "1": 1,
        "2": "na",
        "3": "na",
        "4": "na"
    },
    "col3": {
        "0": 1,
        "1": 3,
        "2": 3,
        "3": 6,
        "4": 3
    },
    "col4": {
        "0": 5,
        "1": "na",
        "2": "9",
        "3": 9,
        "4": "na"
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找最短和最快的功能来实现这一目标。还有其他问题,例如在嵌套字典中将 python 中的字典值从 str 转换为 int ,这些问题建议了这样做的方法,但没有一个涉及字典的“变量嵌套”性质。

任何想法将不胜感激。

python dictionary

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

如何删除输出中的“u”。错误:AttributeError:'dict' 对象没有属性 'encode'

我正在尝试将 JSON 输出存储在变量中。最初我得到了输出,但它前面带有字符“u”(unicode)。下面是初始输出:

{u'imdata': [{u'nvoNws': {u'attributes': {u'dn': u'sys/epId-1/nws', u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'childAction': u''}}}, {u'nvoPeers': {u'attributes': {u'dn': u'sys/epId-1/peers', u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'childAction': u''}}}, {u'nvoEp': {u'attributes': {u'status': u'', u'operState': u'down', u'persistentOnReload': u'true', u'propFaultBitmap': u'', u'hostReach': u'0', u'adminSt': u'disabled', u'holdUpTime': u'0', u'encapType': u'0', u'uid': u'0', u'epId': u'1', u'sourceInterface': u'unspecified', u'descr': u'', u'monPolDn': u'uni/fabric/monfab-default', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'holdDownTimerExpiryTime': u'NA', u'autoRemapReplicationServers': u'no', u'operEncapType': u'0', u'dn': u'sys/epId-1', u'mac': u'00:00:00:00:00:00', u'cfgSrc': u'0', u'childAction': u'', …
Run Code Online (Sandbox Code Playgroud)

python json

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

将PHP数组转换为Python字典格式的字符串

如何将PHP多维数组转换为Python字典格式的字符串?

var_dump($myarray);

array(2) { ["a1"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } ["a2"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } }
Run Code Online (Sandbox Code Playgroud)

php python arrays dictionary function

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