JSON和CSV

JavaScript Object Notatio (JSON)

josn格式需要双引号否则报错""
  1. 读取json
import josn
with open ('data.json','r') as testfile:
	text = josn.read(testfile)
	data = json.loads(text)
	print(data[0])
  1. 写入json
import json
str = """ 
[{
    "name":"bob",
    "gender":"male",
    "birthday": "1992-10-18"
},{
    "name":"小明",
    "gender":"男",
    "birthday" : "1995-11-23"
}]
"""
#dumps()方法将json转换为str,indent为缩进
#ensure_ascii=False和encoding='utd-8'为输出中文
data = json.loads(str)
with open('data.josn','w',encoding='utd-8') as testfile:
    testfile.write(json.dumps(data,indent=2,ensure_ascii=False))

逗号分隔值CSV

  1. 读取csv
import csv
with open('data.csv','r',encoding='utd-8') as testfile:
	reader = csv.reader(testfile)
	for i in reader:
		print(i)
  1. 写入csv
import csv
with open('data.csv','w',encoding='utd-8') as testfile:
	wrirter = csv.writer(testfile):
	writer.writerow(['id','nanme','age'])
	writer.writerow(['10001','Mike','21'])
	writer.writerows([['10002','Bob','19'],['10003','Young','20']])

但是一般情况爬虫都是结构化爬取数据,一般用字典表示

import csv
with open('data.csv','w',encoding='utd-8') as testfile:
	fieldnames = ['id','name','age']	#定义头信息
	writer = csv.DictWriter(testfile,fieldnames=fieldnames)
	writer.writerheader()	#写入头信息
	writer.writerow(['10001','Mike','21'])
	writer.writerows([['10002','Bob','19'],['10003','Young','20']])

若要将数据追加写入将’w’改为’a’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值