python 列表中添加字典元素值覆盖

python 列表中添加字典元素 值覆盖

今天在列表中循环添加元素,遇到了一个问题,后面修改字典元素的值时,之前添加到列表中的字典元素的值也被更改了。
代码如下
代码主要是将一个jl文件转换为csv文件
jl 文件的格式如下
《python 列表中添加字典元素值覆盖》

# coding: utf-8
import csv
import json
import sys
import codecs
from pprint import pprint
from copy import deepcopy

input_file = "junodata.jl"
output_file = "track_data_trimmed.csv"

headers = ["id", "release_artist", "release_cat_num", "release_date",
            "release_genre", "release_label", "release_title",
            "track_name", "track_url"]

index_id = 1
with open(input_file, 'r') as f_jl:
    with open(output_file, 'w+') as csv_f:
        f_csv = csv.DictWriter(csv_f, headers)
        f_csv.writeheader()
        for jl_data in f_jl:
            json_data = json.loads(jl_data)
            # 数据转换
            data = dict()
            row_data = list()
            row_data.clear()
            data['release_artist'] = json_data['artist']
            data['release_cat_num'] = json_data['catalog_number']
            data['release_date'] = json_data['release_date']
            data['release_genre'] = json_data['genre']
            data['release_label'] = json_data['label']
            data['release_title'] = json_data['title']
            for track_infor in json_data['tracks']:
                data['id'] = index_id
                data['track_name'] = track_infor[0]
                data['track_url'] = track_infor[1]
                row_data.append(data)
                index_id += 1
            # 写入CSV
            f_csv.writerows(row_data)
print("Done!")

运行结果如下
《python 列表中添加字典元素值覆盖》

查阅资料
list增加字典元素覆盖问题
Python中向列表添加字典元素的问题
这里主要是变量引用的问题,添加深度复制,解决问题。

# coding: utf-8
import csv
import json
import sys
import codecs
from pprint import pprint
from copy import deepcopy

input_file = "junodata.jl"
output_file = "track_data_trimmed.csv"

headers = ["id", "release_artist", "release_cat_num", "release_date",
            "release_genre", "release_label", "release_title",
            "track_name", "track_url"]

index_id = 1
with open(input_file, 'r') as f_jl:
    with open(output_file, 'w+') as csv_f:
        f_csv = csv.DictWriter(csv_f, headers)
        f_csv.writeheader()
        for jl_data in f_jl:
            json_data = json.loads(jl_data)
            # 数据转换
            data = dict()
            row_data = list()
            row_data.clear()
            data['release_artist'] = json_data['artist']
            data['release_cat_num'] = json_data['catalog_number']
            data['release_date'] = json_data['release_date']
            data['release_genre'] = json_data['genre']
            data['release_label'] = json_data['label']
            data['release_title'] = json_data['title']
            for track_infor in json_data['tracks']:
                data['id'] = index_id
                data['track_name'] = track_infor[0]
                data['track_url'] = track_infor[1]
                row_data.append(data)
                index_id += 1
                # 需要深度复制 否则下次更改时会连之前的列表中的值一起更改
                data = deepcopy(data)
            # 写入CSV
            f_csv.writerows(row_data)
print("Done!")

《python 列表中添加字典元素值覆盖》

    原文作者:Mleeeeeee
    原文地址: https://blog.csdn.net/makingLJ/article/details/80257068
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞