At work, if you often need to read and write CSV text files, and you happen to know python, then you will definitely use the CSV module in python. It is indeed much easier to read and write CSV files using the CSV module. The two most used methods are csv.reader() and csv.writer()< a i=4>. Since the same trivial code needs to be written for each read and write, code disassembly is also required for column processing. Therefore, you can encapsulate these trivial methods and turn them into tools that are more suitable for you.
Custom packaging of CSV modules
Encapsulate the functions that this class needs to have:
1. Read CSV file
- Read full column data row by row
- Read the data of the specified column name row by row
- Get the number of rows of CSV data (counting from the specified header column)
2. Write CSV file
- You can specify the column header to write
class code
Only the function method names are listed below, and the code implementation is omitted due to space issues. The specific code candownload the attachment for viewing.
class CsvOpt:
def __init__(self, file_name:str, rwmode='r', headline:int=1):
def read_file(self):---
def getitem(self, row:int, item:str): ---
def getrow(self, row:int):---
def get_maxrow(self):---
def write_file(self, data_lines:list, head_list:list=[]):---
Method introduction
- __ init__(self, file_name:str, rwmode=‘r’, headline:int=1)
file_name: The name of the csv file to be operated.
rwmode: The read and write mode of the operating file, which is the mode used in the open() method. The default is read mode, which is "r".
headline: Specify the row number of the column header in the csv file. By default, the first row is used as the header.
Function: initialization - read_file(self)
Function without parameters, used to read the entire file content - getitem(self, row:int, item:str)
row: the row number to be read,
item: the Column name
Function: Read the value of the cell in the row row with the column name item - getrow(self, row:int)
row: row number
Function: Get all data in row row - get_maxrow(self)
Function without parameters, returns the number of rows in the csv file starting from the column header position. - write_file(self, data_lines:list, head_list:list=[])
data_lines: data list, for example: [[row data],[row data],[row data ],[row data]]
head_list: specified column header: for example: ['name', 'age', 'gender', 'position'], the default is empty
Function: Write data to the specified csv file
Use case introduction
read file
File name: test.csv
Name | age | address |
---|---|---|
Zhang San | 17 | Shenzhen |
John Doe | 18 | foshan |
Friday | 19 | Shanxi |
King Zheng | 20 | Shanghai |
# -*- coding:utf-8 -*-
from csvopt import CsvOpt
f = "D:\\csvtest\\test.csv"
mycsv = CsvOpt(f) # 初始化文件
mycsv.read_file() # 读取文件
print('按行和指定列名输出:')
for i in range(1, mycsv.get_maxrow() + 1):
print(mycsv.getitem(i, '姓名'), mycsv.getitem(i, '年龄'))
print('按行输出所有列:')
for i in range(1, mycsv.get_maxrow() + 1):
print(mycsv.getrow(i))
The output is as follows:
按行和指定列名输出:
张三 17
李四 18
周五 19
郑王 20
按行输出所有列:
['张三', '17', '深圳']
['李四', '18', '佛山']
['周五', '19', '山西']
['郑王', '20', '上海']
Read the file with the specified header
File name: test.csv
color | |||
---|---|---|---|
White | red | black | green |
20 | 40 | 10 | 15 |
30 | 40 | 20 | 45 |
12 | 20 | 60 | 75 |
Obviously it is not appropriate to use the first row as the header. You need to use the color name row in the second row as the column name to get the value
At this time, you only need to initialize CsvOpt, just specify the line number, and other codes remain unchanged.
# -*- coding:utf-8 -*-
from csvopt import CsvOpt
f = "D:\\csvtest\\test.csv"
mycsv = CsvOpt(f,headline=2) # 初始化文件
mycsv.read_file() # 读取文件
print('按行和指定列名输出:')
for i in range(1, mycsv.get_maxrow() + 1):
print(mycsv.getitem(i, '红色'), mycsv.getitem(i, '绿色'))
print('按行输出所有列:')
for i in range(1, mycsv.get_maxrow() + 1):
print(mycsv.getrow(i))
The output is as follows:
按行和指定列名输出:
40 15
40 45
20 75
按行输出所有列:
['20', '40', '10', '15']
['30', '40', '20', '45']
['12', '20', '60', '75']
Write csv file
# -*- coding:utf-8 -*-
from csvopt import CsvOpt
mycsv = CsvOpt("D:\\test5.csv", 'w')
# 内容数据
wdatalist = [
['辰龙', '50', '太原'], # 第1行数据
['巳蛇', '50', '临汾'], # 第2行数据
['午马', '50', '潍坊'], # 第3行数据
['未时', '50', '烟台'] # 第4行数据
]
mycsv.write_file(wdatalist)
The content of the generated test5.csv file is as follows:
Chenlong | 50 | Taiyuan |
---|---|---|
Si snake | 50 | Linfen |
Wuma | 50 | Weifang |
Not yet | 50 | Yantai |
However, a header is generally added. The specified header is written as follows:
# -*- coding:utf-8 -*-
from csvopt import CsvOpt
mycsv = CsvOpt("D:\\test5.csv", 'w')
# 指定列名
headline = ['名称','数量','地市']
# 内容数据
wdatalist = [
['辰龙', '50', '太原'], # 第1行数据
['巳蛇', '50', '临汾'], # 第2行数据
['午马', '50', '潍坊'], # 第3行数据
['未时', '50', '烟台'] # 第4行数据
]
mycsv.write_file(wdatalist, headline)
The content of the generated test5.csv file is as follows:
name | quantity | city |
---|---|---|
Chenlong | 50 | Taiyuan |
Si snake | 50 | Linfen |
Wuma | 50 | Weifang |
Not yet | 50 | Yantai |