【问题标题】:how to remove Null:[] in Json in Python如何在 Python 中的 Json 中删除 Null:[]
【发布时间】:2021-01-12 13:32:35
【问题描述】:

我一直坚持将 .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))

【问题讨论】:

标签: python json google-bigquery


【解决方案1】:

我通过添加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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多