Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to deal with CSV files in python

Shulou Source: shulou.com Published: 2022-06-02 08:17:21 10月04日 Update

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.

Tags: File object data module processing content newline character article more knowledge article good one line following figure two interactive again function delimiter unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS Redmi Shulou Technology OPPO Reno