我试图使用json_normalize将嵌套字典转换为数据框架。我能够成功地做到这一点,直到我忽略了一些结果的错误。
长话短说-我在python中使用youtube -dl来抓取youtube频道,然后我将结果数据转换为DF。每当youtube视频错误,我忽略它在youtube-dl和继续下一个。我认为这是创建一个坏的字典条目,导致我的json_normalize失败。
这是我如何获得我的频道数据
!pip install --upgrade youtube-dl
import youtube_dl
import pandas as pd
import json
church = input('enter the name of the church')
method = 'y'
channelID = input('enter channel url ending in videos')
with youtube_dl.YoutubeDL({'ignoreerrors' : True}) as ydl:
result = ydl.extract_info(
str(channelID),
download=False,
)
然后用下面的代码片段
将result
转换为DFimport json
data = pd.json_normalize(
result,
record_path =['entries'],
errors='ignore'
)
然而,由于一些视频被跳过由于youtube错误,我得到以下错误每次我运行json_normalize函数
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-101-e3aa31b503f3> in <module>()
19 result,
20 record_path =['entries'],
---> 21 errors='ignore'
22 )
4 frames
pandas/_libs/lib.pyx in pandas._libs.lib.fast_unique_multiple_list_gen()
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py in <genexpr>(.0)
635 """
636 if columns is None:
--> 637 gen = (list(x.keys()) for x in data)
638 sort = not any(isinstance(d, dict) for d in data)
639 columns = lib.fast_unique_multiple_list_gen(gen, sort=sort)
AttributeError: 'NoneType' object has no attribute 'keys'
谁能告诉我我该怎么做来解释那些不好的字典条目?
您可以不预先筛选您的数据列表,如:
data = [x for x in data if x is not None]