首页 > 解决方案 > 如何在 Python 中的 Json 中删除 Null:[]

问题描述

我一直坚持将 .Json 数据流式插入 BigQuery 但 .json 数据中的 Null:[] 会导致这样的错误。 在此处输入图像描述 我在 csv.DictReader 参数上搜索并添加了一些参数,但 Null 数据仍然存在。你能教我如何删除 Null: data in .Json 。太感谢了 。

在此处输入图像描述

def stream_data():
    # BigQuery
    client = bigquery.Client()
    project_id = 'test_project'
    dataset_name = 'test'
    table_name = "test"
    full_table_name = dataset_name + '.' + table_name

    json_rows = [] 
    with open('./test.csv','r') as f:
        for line in csv.DictReader(f,skipinitialspace=False,quoting=csv.QUOTE_NONE):
            line_json = dict(line)
            json_rows.append(line_json)

    errors = client.insert_rows_json(
        full_table_name,json_rows,row_ids=[row['luid'] for row in json_rows]
    ) //stop making record that has same luid duplicately  

    if errors == []:
        print("New rows have been added.")
    else:
        print("Encountered errors while inserting rows: {}".format(errors))

标签: pythonjsongoogle-bigquery

解决方案


我通过添加解决了这个问题del[None]

with open('./test.csv','r') as f:
    for line in csv.DictReader(f):
        del line[None]
        line_json = dict(line)
        json_rows.append(line_json)

推荐阅读