Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the read and write operation of CSV file in python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what is the read and write operation of CSV files in python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Learn how to read and write CSV files from python today.

First look at the test.csv file on my desktop: it's a comma-delimited file format that can be opened with excel or txt:

Next, create a new python file named py3_csv_op.py, and write the operation code in this file:

Import csv#### 's first way is to use csv's reader () writer () # to read test.csv files The character set code UTF-8-sigencode = 'UTF-8-sig'with open (' test.csv','r',encoding=encode) as csv_file: # gets the csv reader read object # the default value of the delimiter parameter comma can not write csv_reader = csv.reader (csv_file,delimiter=',') # Open the test_copy.csv file with open ('test_copy.csv','w') Encoding=encode) as w_file: # get the csv writer write object # in order to distinguish the effect delimiter separator, use-csv_writer = csv.writer (wicked filemt delimiterated written object) # Loop data Write each column to a new file for line in csv_reader: csv_writer.writerow (line) # the second way uses csv's dictionary reader####dictionary writer method to achieve # I prefer the second method with open ('test.csv','r',encoding=encode) as csv_file: csv_reader = csv.DictReader (csv_file,delimiter=',') with open (' test_csv_copy.csv','w' Encoding=encode) as new_file: # the new csv field name is defined here to retain only the name and gender fieldnames= ['name', 'gender'] csv_writer = csv.DictWriter (new_file,fieldnames=fieldnames,delimiter='\ t') # first write the header to csv_writer.writeheader () for line in csv_reader: # each line of data obtained in dictionary form # eg:OrderedDict ([('\ ufeff name') 'Penguin'), ('age', '23'), (' sex', 'female')]) # delete age del line ['age'] csv_writer.writerow (line)

Run the above code to get the following result diagram:

On the python CSV file read and write operation is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report