python - 为什么我会收到错误“AttributeError:模块'pprint'没有属性'PrettyPrinter'
问题描述
我正在尝试使用 pprint 打印出我的 yaml 文件中的值,尽管安装了 pprint 和导入以及所有内容,但我遇到了这个错误
#!/bin/python
import yaml
import subprocess
import os
import pprint
pp = pprint.PrettyPrinter(indent=4)
#pre-requisite-run the script in an empty directory
#read data from the config yaml file
def read_yaml(file):
with open(file, "r") as stream:
try:
config = yaml.safe_load(stream)
# print(config)
except yaml.YAMLError as exc:
print(exc)
print("\n")
return config
d = read_yaml("config.yaml")
#print out contents of the yaml file with a better structure
pp.pprint(d)
在命令行中运行时出现错误消息:
Traceback (most recent call last):
File "pprint.py", line 6, in <module>
import pprint
File "../pprint.py", line 8, in <module>
pp = pprint.PrettyPrinter(indent=4)
AttributeError: module 'pprint' has no attribute 'PrettyPrinter'
解决方案
您的文件被命名为 pprint.py 从而掩盖了您要使用的 pprint 模块。将您的文件重命名为不同的名称。
推荐阅读
- arrays - BigQuery:从 json 对象中提取键,将 json 从对象转换为键值数组
- javascript - 为什么我不能从这个回调函数内部调用一个类方法?错误类型错误:这是未定义的
- python - 使用烧瓶将数据插入 postgresql 时,如何解决 UniqueConstraint 错误?
- javascript - 从 $scope 访问嵌套变量属性的干净方法
- node.js - 为什么“res.sendFile()”函数(Express.js)不能与“pug”文件一起使用?
- java - CompletableFuture 的完成顺序
- coordinates - 从一个 2D 坐标系转换为具有不同 (0,0) 的另一个坐标系
- python - Pandas:前向填充与包含列表对象的系列重叠
- c# - 如何将具有索引的数组映射到具有自定义 JsonConverter 的对象
- java - 如何优化返回列表以进行调试