如何使用Python从Power BI工具的JSON文件中提取字段'Layout'?



我请求一个代码修改,从'中的(嵌套(JSON文件中提取列。PBIX文件(Power BI工具(。详细信息如下:

  • 提取Umberto Grando先生撰写的一些专栏的原始代码:

  • GitHub上的代码:https://github.com/Inzaniak/pybistuff/tree/master/pbixExtractor

  • Medium上的代码说明:https://python.plainenglish.io/extracting-measures-and-fields-from-a-power-bi-report-in-python-1b928d9fb128

  • 为了方便您,我在Google Drive中提取了布局文件:https://drive.google.com/drive/folders/1Z5cqgE-iuS0__G5hCl7Ge-MZW9WKmJu7?usp=sharing

请求:

  • 我需要在上面的GitHub代码中以类似的格式提取'visualType'字段,用于业务文档。能够在表中自动提取这样的参数将节省文档中数小时的时间

我试过了:

  • JS BeautifyJSON转储,JSON加载,JSON规范化,不同的编码类型,在代码中添加['visualTypes'],但不知道如何附加。我也很难理解这里JSON文件的结构

其他尝试:

!cd "path/Layout"
import json
# Opening JSON file
f = open('path/Layout', encoding='utf-16 le')
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
print(i)

###输出:###

  • id sections pods config reportId resourcePackages layout优化publicCustomVisuals

这被低估的主要原因是,当你试图解决这个问题时,没有发布任何代码或错误。

当我尝试时,我得到了以下错误:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

以及:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 9380: invalid start byte

你很可能会在谷歌上找到很多答案:

当你在json.load()中看到字节错误时,很可能是编码,所以chardet会帮助你:

#!/usr/bin/env python3
import json
import chardet
raw = "C:\Users\mobj\Downloads\Layout"
with open(raw, 'rb') as FI:
print(chardet.detect(FI.read()))
dat = {}
# encoding from chardet:
with open(raw, 'r', encoding='utf-16le', errors='ignore') as FI:
dat = json.load(FI)
print(json.dumps(dat, indent=2, sort_keys=True))

最新更新