Example of python using the csv module to read and write csv format files
import csv class HandleCsv: ''' csv file handling classes ''' def __init__(self, filename): ''' Constructor :param filename: csv filename ''' = filename def get_data(self): ''' Get all the data in the csv :return: list of nested dictionaries ''' with open(, mode='r', encoding='utf-8') as f: cb = (f) # Instantiate the reader object header = next(cb) # Get the table header and move the pointer to the next line list_dict = [] for row in cb: list_dict.append(dict(zip(header, row))) return list_dict def get_one_row(self, row): ''' Get a single row of data :param row: the specified row number :return: the data corresponding to the row number ''' return self.get_data()[row - 1] def write_csv(self, headers, values, data_type, mode='w'): ''' Write data to csv to file :param headers: table headers: list type :param values: table data: 1. list of nested tuples; 2. list of nested dictionaries :param data_type: incoming data type: 1. 'tuple';2.'dict' :param mode: Write mode, default is "w". :return. ''' with open(file=, mode=mode, encoding='utf-8', newline='') as f: if data_type == 'tuple': writer = (f) # Instantiate the writer object (headers) # Write to table header (values) # Write data elif data_type == 'dict': writer = (f, headers) # Instantiate the DictWriter object () # Write to table header (values) # Write data else: print("Wrong data type, please confirm!")
The above is python use csv module to read and write csv format file example of the details, more about python read and write csv file information please pay attention to my other related articles!
Related articles
Python example of creating an exe runner and screenshot tool in detail
In this article we will explore how to use Python and wxPython to create a powerful and practical desktop application, you can traverse the specified folder in all the EXE files, interested partners can learn about it!2024-10-10Example of python plotting a double y-axis line chart and a single y-axis bivariate bar chart
Today, I'd like to share with you an example of python drawing a double Y-axis line graph and a single Y-axis bivariate bar chart, which has a good reference value and hopefully will be helpful to you. Together follow the editor over to see it2019-07-07How python stores local images in word
This article mainly introduces python how to store local pictures in word, want to understand the docx module students, you can refer to the following2021-04-04Python wrapped database connection pooling details
This article introduces the Python encapsulated database connection pool, the article around the subject matter of the content to develop a detailed introduction, with certain reference value, the need for partners can refer to it!2022-06-06Django paging feature implementation code details
In this article, I have organized a paging function on Django implementation code and related knowledge content, friends can follow the need to learn reference.2019-07-07Python manipulates excel to automate work
This article is mainly for you to introduce in detail how to operate python excel so that the work of automation, with certain reference value, interested partners can refer to a2019-08-08Usage of the elasticsearch_dsl module in python
This article mainly introduces the use of python elasticsearch_dsl module, elasticsearch-dsl is based on elasticsearch-py package to achieve, provides a simpler way to operate elasticsearch2022-09-09Code for assisted development with SQLAlchemy in flask
In, Django, Flask, Tornado, comes with a lack of ORM functionality, it is recommended that you use SQLAlchemy to assist in development.2013-02-02Python requests and aiohttp speed comparison code example
This article introduces the Python requests and aiohttp speed comparison code example, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, you can refer to the next!2020-07-07Django to achieve the function of the download file example
This article introduces the Django download file to achieve the function of the example, I think it's pretty good, now share it with you, but also to give you a reference. Follow the editor over to take a look at it2018-03-03