我有一个带有元组键的嵌套字典结构。这是我使用 pprint 漂亮地打印字典时条目的样子:
...
('A', 'B'): {'C': 0.14285714285714285,
'D': 0.14285714285714285,
'E': 0.14285714285714285,
'F': 0.14285714285714285,
'G': 0.14285714285714285,
'H': 0.14285714285714285,
'I': 0.14285714285714285},
...
Run Code Online (Sandbox Code Playgroud)
它非常漂亮,但我想通过从浮点数中减少一些额外的数字来进一步定制它。我以为可以通过子类化来实现,pprint.PrettyPrint但我不知道如何实现。
谢谢。
我在用org.clojure/tools.logging。我想要一个可以很好地打印日志的函数,但我找不到它。例如我要打印的内容是
{:status 401,
:headers {"Content-Type" "application/octet-stream"},
:body {:error "You don't login."}}
Run Code Online (Sandbox Code Playgroud)
但我调用函数名称是info,我明白了
{:status 401, :headers {Content-Type application/octet-stream}, :body {:error You don't login.}}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 macbook pro 上安装 NLP 套件,该套件已更新到最新的软件版本 Catalina 10.15.6。到目前为止,我已经安装了Anaconda 3.8,创建了一个3.7版本的NLP环境conda create -n NLP python=3.7,并激活了NLP环境conda activate NLP。
我的下一步是使用以下命令安装所有写在“requirements.txt”文件中的 python 包pip install -r requirements.txt。但是,它显示了以下消息:“错误:找不到满足 pprint 要求的版本(来自 -r requirements.txt(第 67 行))(来自版本:无)错误:没有找到 pprint 的匹配分布(来自 -r requirements.txt(第 67 行))”
我也尝试单独安装软件包,但是,出现相同的错误消息。
任何意见,将不胜感激!如果我可以提供任何其他信息,请告诉我。
我不明白这个概念.有人可以给我一些例子来展示python中pprint的宽度概念吗?
我想找到一些方法在json中打印出curl的输出.我为此目的写了一个简短的python脚本,但它不适用于管道我也不想使用子进程并从它们运行curl:
所以python:
#!/usr/bin/python
import simplejson
from pprint import pprint
import sys
print pprint(simplejson.loads(sys.argv[1]))
Run Code Online (Sandbox Code Playgroud)
而json的信息是:
{"response": {"utilization": {"eic": [{"date": "2012.03.06", "usage": []}, {"date": "2012.03.07", "usage": [{"srvCode": "SVC302", "upload": 267547188, "api-calls": {"fileGetInfo": 30, "getUserStorageQuota": 0, "setUserStorageQuota": 0, "fileUploadFlashInit": 31, "getAPISessionUser": 0, "setFileAccessControl": 0, "fileGetPreviewUrl": 0, "fileStartMultipartUpload": 0, "getServiceQuota": 0, "fileGetPreviewUrlsForBunch": 10, "xcodingGetStreamUrl": 0, "getSessionTimeLimit": 0, "fileGetCoversUrlsForBunch": 27, "makePreviews": 0, "setServiceQuota": 0, "getAPISessionTrusted": 3, "getFileAccessControl": 0, "xcodingGetFormats": 0, "getQuotaNotificationEmail": 0, "fileGetDownloadUrl": 0, "xcodingGetStreamInfo": 0, "fileUploadDone": 30, "getLocalServiceUtilization": 9, "getServiceUtilization": 0, "fileDelete": 19, "setSessionTimeLimit": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用该pprint模块检查Python中的一些变量,我可以愉快地使用交互式shell和下面的代码:
import pprint
pp = pprint.PrettyPrinter()
stuff = ['cakes','bread','mead']
pp.pprint(stuff)
Run Code Online (Sandbox Code Playgroud)
但是,当我把上面的内容放入pprint.py并使用它运行时python pprint.py我得到错误:
$ python dev/pars/pprint.py
Traceback (most recent call last):
File "dev/pars/pprint.py", line 1, in ?
import pprint
File "/home/origina2/dev/pars/pprint.py", line 2, in ?
pp = pprint.PrettyPrinter()
AttributeError: 'module' object has no attribute 'PrettyPrinter'
Run Code Online (Sandbox Code Playgroud)
与交互式shell相比,从文件运行Python代码时调用模块的方式有何不同?
打印时遇到问题namedtuple:
Info = namedtuple('Info', ['type', 'value', 'x', 'y'])
Run Code Online (Sandbox Code Playgroud)
以便这些值对齐并在它们之间留有空格(填充),例如像这样:
Info( type='AAA', value=80000, x=16.4, y=164.2 )
Info( type='A', value=78, x=1.5, y=11.3 )
Info( type='ABBCD', value=554, x=11.7, y=10.1 )
Info( type='AFFG', value=1263, x=121.3, y=12.8 )
Run Code Online (Sandbox Code Playgroud)
理想情况下,没有逗号。我已经尝试pprint并尝试使用此处的_asdict建议进行打印,但没有成功。与我无法使其与命名元组一起工作的相同。任何想法或示例代码?format
我正在尝试pprint在字典上使用Python ,但由于某种原因它不起作用.这是我的代码(我使用PyCharm Pro作为我的IDE):`
from pprint import pprint
message = "Come on Eileen!"
count = {}
for character in message:
count.setdefault(character, 0)
count[character] += 1
pprint(count)
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
{' ': 2, '!': 1, 'C': 1, 'E': 1, 'e': 3, 'i': 1, 'l': 1, 'm': 1, 'n': 2, 'o': 2}
Run Code Online (Sandbox Code Playgroud)
任何帮助都将不胜感激.
我想以视觉方式查看下面的结果是否是我需要的:
import nltk
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]
pattern = """NP: {<DT>?<JJ>*<NN>}
VBD: {<VBD>}
IN: {<IN>}"""
NPChunker = nltk.RegexpParser(pattern)
result = NPChunker.parse(sentence)
Run Code Online (Sandbox Code Playgroud)
来源:https : //stackoverflow.com/a/31937278/3552975
我不知道为什么我不能漂亮_打印result.
result.pretty_print()
Run Code Online (Sandbox Code Playgroud)
错误是这样写的TypeError: not all arguments converted during string formatting。我使用Python3.5,nltk3.3。
我正在尝试漂亮的打印字典,它可能会变得很大,我想让它尽可能地可读。尽管它看起来仍然不像我手动编写它的方式(需要时使用新行和缩进)。
我正在尝试使用这样的格式(2 个空格缩进):
{
'a': {
'1': [],
'2': []
},
'b': {
'1': [],
'2': [],
}
}
Run Code Online (Sandbox Code Playgroud)
现在最初的 dict 看起来像这样(不使用 pprint):
{'test': {'0.2.0': {'deploy': {'some.host.com': {'outputs': [], 'inputs': []}},
'release': {'some.git': {'outputs': [], 'inputs': []}}},
'0.1.0': {'deploy': {'some.host.com': {'outputs': [], 'inputs': []}},
'release': {'some.git': {'outputs': [], 'inputs': []}}}},
'stage': {'0.1.0': {'deploy': {'stage.com': {'outputs': [], 'inputs': []}},
'release': {'stage.git': {'outputs': [], 'inputs': []}}}}}
Run Code Online (Sandbox Code Playgroud)
与:pprint.pprint(my_dict),它看起来像:
{'stage': {'0.1.0': {'deploy': {'stage.com': {'inputs': [], 'outputs': []}},
'release': {'stage.git': {'inputs': [], 'outputs': …Run Code Online (Sandbox Code Playgroud) pprint ×10
python ×9
python-3.x ×2
anaconda ×1
bash ×1
clojure ×1
concept ×1
conda ×1
formatting ×1
json ×1
logging ×1
namedtuple ×1
nltk ×1
python-3.4 ×1
tree ×1
width ×1