python读取csv数据写入json,删除指定数据

import json

with open("C:\\Users\\12804\\Desktop\\example.json", encoding="utf-8") as f:
    full_data = json.load(f)  # 取出json所有数据放入字典中
    print(full_data)
    f.close()

for i in range (len(full_data)):
    del full_data[i]["Affected Resources"]  # 删除字典中指定键值对 并将新的字典覆盖原来的json中内容

with open("C:\\Users\\12804\\Desktop\\example.json", 'w+', encoding="utf-8") as f:  # 覆盖写入
    json.dump(full_data, f, indent=2, ensure_ascii=False)
    f.close()
好的,下面是使用Python读取和输出CSVJSON数据的示例代码: 首先,我们将演示如何读取CSV文件。我们可以使用csv库来读取写入CSV文件。以下是一个示例代码: ```python import csv # 读取CSV文件 with open('example.csv', newline='') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in reader: print(', '.join(row)) ``` 在上面的代码,我们使用csv.reader函数读取CSV文件,并遍历每一行数据并输出。 接下来,我们将演示如何写入CSV文件。以下是一个示例代码: ```python import csv # 写入CSV文件 with open('example.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow(['Name', 'Age', 'Gender']) writer.writerow(['John', '25', 'Male']) writer.writerow(['Mary', '30', 'Female']) ``` 在上面的代码,我们使用csv.writer函数写入CSV文件,并将数据写入文件。 接下来,我们将演示如何读取JSON文件。我们可以使用json库来读取写入JSON文件。以下是一个示例代码: ```python import json # 读取JSON文件 with open('example.json') as json_file: data = json.load(json_file) for p in data['people']: print('Name: ' + p['name']) print('Age: ' + str(p['age'])) print('Gender: ' + p['gender']) ``` 在上面的代码,我们使用json.load函数读取JSON文件,并遍历每个对象并输出。 接下来,我们将演示如何写入JSON文件。以下是一个示例代码: ```python import json # 写入JSON文件 data = {} data['people'] = [] data['people'].append({ 'name': 'John', 'age': 25, 'gender': 'Male' }) data['people'].append({ 'name': 'Mary', 'age': 30, 'gender': 'Female' }) with open('example.json', 'w') as outfile: json.dump(data, outfile) ``` 在上面的代码,我们使用json.dump函数将数据写入JSON文件。 希望这个示例代码可以帮助您!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值