Python字典键中的空格 [英] Spaces in Python Dictionary Keys

查看:990
本文介绍了Python字典键中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以在Python字典键中有空格,但是这被认为是糟糕的编程?



修改澄清
在我正在做的一个项目中,我正在从Apache的 mod_status 中解析记分板输出的内容(见下面的示例输出)。我只是想弄清楚最佳做法。我应该这样做:

  workers = {'W':1,
'_':9,
...}

或这个:

  workers = {'发送回复':1,
'等待连接':9,
...}

示例 mod_status 输出:

  ________ W ____._ ..................... 
......................... .......................................
....... .................................................. .......
....................................... .........................
记分板键:
_等待连接,S启动 R读取请求,
W发送回复KKeepalive(读取),DDNS查找,
C关闭连接L记录G正常整理,
我空闲的工作人员清理,。没有当前进程的开放槽


解决方案

pylint ,以检查您的代码是否符合PEP8:



所以我尝试了如下:

  M_D1 = {foo bar:1,a bc :2,:3} 

而pylint给了我10/10的评分,所以我猜钥匙中使用的空格没有问题。


I know you can have spaces in Python dictionary keys, but is that considered bad programming? I couldn't find anything in the PEPs about this.

Edit for clarification: On a project I'm doing, I'm working on something that parses the scoreboard output from Apache's mod_status (see example output below.) I'm just trying to figure out the best practice. Should I end up with this:

workers = {'W': 1,
           '_': 9,
           ...}

or this:

workers = {'Sending Reply': 1,
           'Waiting for Connection': 9,
           ...}

Example mod_status output:

_....___W____._.................................................
................................................................
................................................................
................................................................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process

解决方案

You can use tools like pylint to check whether your code is PEP8 compatible or not :

so I tried something like :

M_D1 = {" foo bar ":1, " a bc":2, " ":3}

and pylint gave me 10/10 rating, so I guess it has no issues with the spaces used in keys.

这篇关于Python字典键中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆