使用csv库处理csv文件
1、读取
import csv
def read(path):
'''使用reader读取数据'''
reader = csv.reader(open(path, encoding='utf-8'))
for row in reader:
print(row)
def dictread(path):
'''使用DictReader读取数据'''
reader = csv.DictReader(open(path, encoding='utf-8'))
for row in reader:
print(row, row['姓名'])
def get_column(path, col):
'''获取某一列数据'''
reader = csv.DictReader(open(path, encoding='utf-8'))
for row in reader:
print(row[col])
2、说明
1、reader函数,会返回一个生成器,可以从其中解析出csv的内容,返回结果以行为单位。
2、DictReader,和reader函数类似,也是返回一个生成器,但是返回的每一个单元格都放在一个字典的值内,而这个字典的键则是这个单元格的标题(即列头)。
3、写入
import csv
def write_list(path, lst):
'''写入单行'''
writer = csv.writer(open(path, 'a', newline=''))
writer.writerow(lst)
def write_lists(path, lst):
'''写入多行'''
writer = csv.writer(open(path, 'a', newline=''))
writer.writerows(lst)
4、说明
1、writer.writerow(list)用来写入单行数据,writer.writerows(list)用来写入多行数据。
2、写入的时候添加newline='',不然写入的每一行后面都会有一个空行
使用pandas库处理csv文件
1、读取
import pandas as pd
def read_all(path):
df = pd.read_csv(path)
print(df)
def read_start_to_end(path, start, end):
'''读取从start到end行的数据,不包括end'''
df = pd.read_csv(path)
print(df[start:end])
def read_some_rows(path, *rows):
'''读取指定的某几行数据'''
df = pd.read_csv(path)
lst = pd.DataFrame(df, index=rows)
print(lst)
def read_some_lines(path, *lines):
'''读取指定的某些列的数据'''
df = pd.read_csv(path)
lst = pd.DataFrame(df, columns=lines)
print(lst)
def read_some_rows_lines(path, rows, lines):
'''读取指定的某些行中某些列的值'''
df = pd.read_csv(path)
lst = pd.DataFrame(df, index=rows, columns=lines)
print(lst)
def read_head(path, l=None):
'''从头部开始返回指定行数,默认为5'''
df = pd.read_csv(path)
print(df.head(l))
def read_tail(path, l=None):
'''从尾部开始返回指定行数,默认为5'''
df = pd.read_csv(path)
print(df.tail(l))
def read_sample(path, l=None):
'''随机返回指定行数,默认为1'''
df = pd.read_csv(path)
print(df.sample(l))
def sort(path, asce=True, *columns):
'''按指定列进行排序,asce=True为升序,asce=False为降序'''
cols = list(columns)
df = pd.read_csv(path)
print(df.sort_values(by=cols, ascending=True))
def find_by_age(path, age):
'''根据年龄进行查找'''
df = pd.read_csv(path)
print(df[df['年龄'] == age])
def find_row_line(path, name, lines):
'''根据条件查找指定的列的数据,如根据姓名查找年龄'''
df = pd.DataFrame(pd.read_csv(path))
print(df.loc[df['姓名']==name, lines])
2、修改
import pandas as pd
def update_age(path, name, age):
'''根据名称修改年龄'''
df = pd.DataFrame(pd.read_csv(path))
df.loc[df['姓名'] == name, '年龄'] = age
print(df.loc[df['姓名']==name, '年龄'])
3、删除
import pandas as pd
def drop_columns(path, *columns):
'''删除指定列'''
cols = list(columns)
df = pd.read_csv(path)
df2 = df.drop(cols, axis=1)
print(df2)