# -*- coding: UTF-8 -*-
import csv
# 1使用csv.writer()创建
headers = ['学号', '姓名', '分数']
rows = [('202001', '张三', '98'),
('202002', '李四', '95'),
('202003', '王五', '92')]
with open('score1.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
# 2使用csv.Dictwriter()创建
headers = ['学号', '姓名', '分数']
rows = [{'学号': '202001', '姓名': '张三', '分数': '98'},
{'学号': '202002', '姓名': '李四', '分数': '95'},
{'学号': '202003', '姓名': '王五', '分数': '92'}]
with open('score2.csv', 'wb') as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
writer.writerows(rows)
# 3使用writelines()创建
headers = ['学号,姓名,分数', '\n']
csv = ['202001,张三,98', '\n',
'202002,李四,95', '\n',
'202003,王五,92']
with open('score3.csv', 'wb') as f:
f.writelines(headers) # write() argument must be str, not tuple
f.writelines(csv)
open(‘score3.csv’, ‘wb’)改为open(‘score3.csv’, ‘w’)则在每行数据下会增加1个空白行
import csv
# 1使用csv.writer()创建
headers = ['学号', '姓名', '分数']
rows = [('202001', '张三', '98'),
('202002', '李四', '95'),
('202003', '王五', '92')]
with open('score1.csv', 'w', encoding='utf8', newline='') as f:
writer = csv.writer(f)
# 写入columns_name
writer.writerow(headers)
# 写入多行用writerows
writer.writerows(rows)
# 2使用csv.Dictwriter()创建
headers = ['学号', '姓名', '分数']
rows = [{'学号': '202001', '姓名': '张三', '分数': '98'},
{'学号': '202002', '姓名': '李四', '分数': '95'},
{'学号': '202003', '姓名': '王五', '分数': '92'}]
with open('score2.csv', 'w', encoding='utf8', newline='') as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
writer.writerows(rows)
# 3使用writelines()创建
headers = ['学号,姓名,分数', '\n']
csv = ['202001,张三,98', '\n',
'202002,李四,95', '\n',
'202003,王五,92']
with open('score3.csv', 'w', encoding='utf8', newline='') as f:
f.writelines(headers) # write() argument must be str, not tuple
f.writelines(csv)
import csv
import numpy as np
import pandas as pd
# 1使用pandas.read_csv()读取
my_matrix = pd.read_csv('score1.csv') # ,header=None,index_col=None)
'''
header : int or list of ints, default ‘infer',指定行数用来作为列名,数据开始行数。如果文件中没有列名,则默认为0,
index_col : int or sequence or False, default None,用作行索引的列编号或者列名
'''
print("pd.read_csv('score1.csv')")
print(my_matrix)
print(my_matrix.shape)
print('-'*40)
my_matrix = pd.read_csv('score.csv', header=None, index_col=None)
print("pd.read_csv('score.csv', header=None, index_col=None)")
print(my_matrix)
print(my_matrix.shape)
print('-'*40)
my_matrix = pd.read_csv('score.csv', header=0, index_col=0)
print("pd.read_csv('score.csv', header=0, index_col=0)")
print(my_matrix)
print(my_matrix.shape)
# 将数据类型改为np.array型,代码如下:
print('-'*60)
my_matrix = pd.read_csv('score.csv') # ,header=0,index_col=0)
my_matrix = np.array(my_matrix)
print(my_matrix)
print(my_matrix.shape)
print(my_matrix[0][0])
打印输出:
H:\ProgramData\Anaconda3\python.exe C:\Users\Lenovo\Desktop\csv_python\c2.py
pd.read_csv('score1.csv')
学号 姓名 分数
0 202001 张三 98
1 202002 李四 95
2 202003 王五 92
(3, 3)
----------------------------------------
pd.read_csv('score.csv', header=None, index_col=None)
0 1 2
0 学号 姓名 分数
1 202001 张三 98
2 202002 李四 95
3 202003 王五 92
(4, 3)
----------------------------------------
pd.read_csv('score.csv', header=0, index_col=0)
姓名 分数
学号
202001 张三 98
202002 李四 95
202003 王五 92
(3, 2)
------------------------------------------------------------
[[202001 '张三' 98]
[202002 '李四' 95]
[202003 '王五' 92]]
(3, 3)
202001
Process finished with exit code 0
# 2使用csv.reader()进行读取
with open('score.csv', "r", encoding='utf8', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# print(row[0]) # 如果想获取某一列,可以通过指定的列标号来查询
输出:
['学号', '姓名', '分数']
['202001', '张三', '98']
['202002', '李四', '95']
['202003', '王五', '92']
# 3 使用csv.DictReader()读取csv文件
with open('score.csv', "r",encoding='utf8',newline='') as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
# print(row['学号']) # 如果想获取某一列,可以通过指定的标题来查询
输出:
{'学号': '202001', '姓名': '张三', '分数': '98'}
{'学号': '202002', '姓名': '李四', '分数': '95'}
{'学号': '202003', '姓名': '王五', '分数': '92'}
csv模块时python自带的,因此不需要安装就可以导入它
要用csv模块读取csv文件,要用open()函数打开,这将返回一个reader对象供你使用。请注意,不能直接将文件名字符串传递给csv.reader()函数。
要访问reader对象中的值,最直接的方法就是将它转换成一个普通Python列表,即将它传递给list()。
reader对象可理解为列表的列表,可用表法是对象 redaer对象[row][col] 来访问特定行和列的值。