当前位置: 代码迷 >> 综合 >> 【Python】 将数据写入 csv 文件中并读取
  详细解决方案

【Python】 将数据写入 csv 文件中并读取

热度:29   发布时间:2023-11-01 02:55:35.0

将数据存放到csv文件中

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)

 

  相关解决方案