将数据存放到csv文件中
默认newline参数的值为\n,在writerow输出一行换行后,还会再次换行,就会导致两条数据之间存在空行的问题,将newline设置为空就可以解决这个文件
import csv # 加载csv库
csvfile = open('test.csv', 'w',newline='', encoding="utf-8")
writer = csv.writer(csvfile)
writer.writerow(['岗位信息', '工资']) # 标题,写入一行
# data为数据
writer.writerows(data) # 数据,写入多行
csvfile.close()
读取csv中的数据内容
# 读取CSV文件,(“文件路径”,编码格式)
csvfile = open("test.csv",encoding="utf-8")
content = csv.reader(csvfile)
rows = [row for row in content]
print(rows)