爬虫常用本地存储形式(TXT,JSON,CSV)

306次阅读
没有评论

爬虫常用本地存储形式(TXT,JSON,CSV)

一,TXT存储

TXT格式读写在本站中的详细解释

1.读取/写入模式

爬虫常用本地存储形式(TXT,JSON,CSV)
爬虫常用本地存储形式(TXT,JSON,CSV)

2.使用演示

#常规写法 file = open('explore.txt','a',encoding='utf-8') file.write ('\n'.join([question, author, answer])) file.write('\n'+ '= '*50 +'\n') file. close()

#不用关闭指针的简化写法 with open('./song.txt','w',encoding='utf-8') as fp: fp.write(resp.text)

二,JSON存储

1.序列与反序列化函数区别

爬虫常用本地存储形式(TXT,JSON,CSV)
爬虫常用本地存储形式(TXT,JSON,CSV)

2.使用演示

######读取json数据###### import json

str = json格式数据

data = json.loads(str) print(data)

with open("douban.json","r",encoding="utf-8") as f: res = json.load(f) print(res) print(type(res))

######保存json数据###### import json

#json.loads把json字符串转化为python类型 data = json.loads(html.content.decode())

#json.dumps能够把pyton类型转化为json类型(写文件时用) with open("html.json", "w", encoding='utf-8') as f: f.write(json.dumps(data, ensure_ascii=False, indent=2)) # 默认为True为ascii编码,当内容有中文字符时就要设置为假 indent参数表示下一级距离上一级都空两格 f.write(str(data))#也可以实现存入信息,但是格式就没有json那么好看

三,CSV存储

############################常规写法################### #常规写入文件,逐行写入 import csv

f = open("data.csv", mode="w",encoding = "utf-8") csvwriter = csv.writer(f) for row_data in result: csvwriter.writerow(row_data) #常规读取文件,逐行读 import csv

f = open("data.csv", mode="r",encoding = "utf-8") csvreader = csv.reader(f) for row_data in csvreader: print(row_data)

############################简化写法################### #简化写,多行同时写 import csv

with open("data.csv", mode="w",encoding = "utf-8") as csvfile: csvwriter = csv.writer(csvfile) csvwriter = csv.writer(csvfile,delimiter = ' ') #里在初始化写入对象时传入 delimiter 为空格,此时输出结果的每一列就是以空格分隔了,默认是逗号分割 csvwriter.writerow(['id','name','age']) #单次写单行数据 csvwriter.writerow(['1','mile','15'],['2','song','7'],['3','wang','23']) #单次写多行数据

#字典形式数据的写入方式,读取用csv.DictReader data_columns = ['id','name','age'] writer = csv.DictWriter(csvfile,fieldnames = data_columns) writer.writeheader() #写入头信息 writer.writerow({'id':'1','name':'mike','age':'15'})

#简化读 import csv

with open('data.csv','r',encoding = 'utf-8') as csvfile: csvreader = csv.reader(csvfile) for row_data in csvreader: print(row_data)

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-27发表,共计2156字。
新手QQ群:570568346,欢迎进群讨论 Python51学习