Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how to remove Null:[] in Json in Python

I've been stuck in streaming insert .Json data into BigQuery but Null:[] in .json data causes error like this . enter image description here I googled and added some parameters on csv.DictReader parameters but Null data are still there. Could you teach me how to remove Null: data in .Json . Thank you so much .

enter image description here

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved this problem by adding 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...