python读写csv

一、使用内置模块csv

1. 使用字典的形式取数据
import csv

with open("des_file.csv", 'r', encoding='utf-8)as f:
	# 将每一行转化为以第一行索引为键,对应单元格内容为值的字典
	f_csv = csv.DictReader(f)
	for row in f_csv:
		value = row['key']
		print(value)
2. 使用列表的形式取数据
import csv
# endoing='utf-8' 中文编码问题
with open("des_file.csv", 'r', encoding='utf-8)as f:
	# 将每一行数据组成一个列表,每个单元格是一个列表元素
	f_csv = csv.reader(f)
	# 指定列之间的分隔符--delimiter参数
    writer = csv.writer(csvfile, delimiter=',')
	print(type(f_csv))  # <class '_csv.reader'>  对象
	for row in f_csv:
		print(row) # 第一个元素列表是表头
		print(type(row)) # <class 'list'>
3.列表数据写入csv
import csv
# endoing='utf-8' 中文编码问题
with open('des_file.csv', 'w', endoing='utf-8') as csvfile:
    # 初始化写入对象
    writer = csv.writer(csvfile)
    # 修改列之间的分隔符--delimiter参数
    writer = csv.writer(csvfile, delimiter=' ')
    # writerow 写入一行数据,调用一次写入一行
    writer.writerow(['key1', 'key2', 'key3'])  # 表头
    writer.writerow(['key1', 'key2', 'key3'])  # 第一行
    # writerow,调用一次写入多行  传入多维列表
    writer.writerow([['key1', 'key2', 'key3'],['key1', 'key2', 'key3']])
4.字典数据写入csv
import csv
# endoing='utf-8' 中文编码问题
with open('des_file.csv', 'w', endoing='utf-8') as csvfile:
    fieldnames = ['key1', 'key2', 'key3']  # 表头
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()    # 先写入表头
    # 以字典形式的数据写入一行
    writer.writerow({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})
    writer.writerow({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})
    writer.writerow({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})
5.追加文件

打开csv文件,将“w”写模式改为“a”追加模式,此时不应该写入表头数据

import csv
# endoing='utf-8' 中文编码问题
with open('des_file.csv', 'w', endoing='utf-8') as csvfile:
    fieldnames = ['key1', 'key2', 'key3']  # 表头
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    # 以字典形式的数据写入一行
    writer.writerow({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})

二、使用一般文件读取方法

1. with open
# endoing='utf-8' 中文编码问题
with open("des_file.csv", 'r', endoing='utf-8')as f:
	# 按行读取,单元格之间以逗号隔开(使用文本阅读器查看csv文件时显示的格式)
	for line in f:
		print(line)

三、pandas处理csv

1.读取

pip install pandas
import pandas as pd
data = pd.read_csv("des_file.csv")
print data
import pandas as pd
data = pd.read_table("des_file.csv",sep=",")
print data

2.写入

import pandas as pd
df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                   'mask': ['red', 'purple'],
                   'weapon': ['sai', 'bo staff']})
df.to_csv(index=False)

参考网址
https://www.py.cn/jishu/jichu/12705.html
https://www.py.cn/spider/advanced/14381.html
https://blog.csdn.net/xz1308579340/article/details/81106310
https://www.jianshu.com/p/339347ea516d
pandas处理csv
https://blog.csdn.net/liufang0001/article/details/77856255
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
https://blog.csdn.net/qq_42052864/article/details/81607400 比较全

3、出现的一些错误

1.读取csv文件有中文路径,报错 OSError: Initializing from file failed

虽然使用的是Python3,但是仍读取文件失败。
在读取文件的时候加上参数 engine='python’

data = pd.read_csv(csv_path, sep=',', engine='python')

https://blog.csdn.net/qq_35318838/article/details/80564938 报错原因分析,内部代码有c

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值