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

How to deal with CSV files in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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

1. Environment

Python3.8

Pycharm2020.1

two。 Read

Instance data of this issue

Haha,18,10.0jiji,16,12.1lala,17,11.9papa,11,13.3

First import csv module, do not need to install, python comes with.

Import csv

To read csv file data with the csv module, you need to create a Reader object, and Reader can traverse every line of the file.

Note: the Reader object can only be iterated once, and if you want to do it again, you need to recreate it.

File = open (".csv") reader = csv.reader (file) data = list (reader) print (data)

The following is how to operate in the interactive interface.

Using the Reader object to traverse the data, the line to which the Reader.line_num flag is currently traversed.

Import csvfile = open (".csv") reader = csv.reader (file) for row in reader: print (("line {}" + str (row)) .format (reader.line_num))

3. Write

The Writer object is required to write the data to the CSV file. As with reading, import the csv module first, and then open the file.

Encoding is coded.

Newline equals a null character, and if not set, the line spacing will become as shown in the following figure on Windows systems.

Import csvfile = open ('.csv',' walled, encoding='utf-8', newline='')

To create a Writer object, csv.writer () has two parameters to note.

Delimiter: the cell delimiter, which defaults to a comma, can be changed to something else.

Lineterminator: the line Terminator, which is newline by default, can be modified by yourself.

Writer = csv.writer (file)

Write data, using writer.writerow (), which takes a list and returns the number of characters written to the line (including newline characters)

Writer.writerow (['',' 18,'10. 0]) writer.writerow (['jiji',' 16,'12. 1]) writer.writerow (['lala',' 17, '11.9]) writer.writerow ([' papa','11, '13.3])

Write the complete source code

Import csvfile = open ('.csv',' walled, encoding='utf-8') writer = csv.writer (file) writer.writerow (['',' 18, '10.0']) writer.writerow ([' jiji','16', '12.1']) writer.writerow (['lala',' 17', '11.9']) writer.writerow (['papa',' 11' '13.3']) file.close () on how to deal with CSV files in python, that's it. I hope the above content can be of some help to you and 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