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 use pandas to read, write and append csv files in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how python uses pandas to read and write and append csv files. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Csv file

CSV file is one of the most commonly used file storage methods. Comma separated values (Common-Separated Values,CSV) files store table data in plain text (note: delimiters can also be other characters). Plain text indicates that the file is a sequence of characters and does not contain data that must be interpreted like binary numbers.

An CSV file consists of any number of records separated by some kind of newline character; each record consists of several fields separated by characters such as commas or strings.

First, create a csv file

Open it with notepad as shown

Second, read and write csv files 1. Basic pythonimport csvwith open ('supplier_data.csv','r') as f: reader = csv.reader (f) for row in reader: print (row)

2.pandasimport pandas as pddf = pd.read_csv ('supplier_data.csv') print (df)

Third, append csv file 1. Basic pythonimport csvwith open ('supplier_data.csv','a') as f: writer = csv.writer (f) writer.writerow.

At this point, we find that the added data will be empty, and to solve this problem, we need to use newline=''

Import csvwith open ('supplier_data.csv','a',newline='') as f: writer = csv.writer (f) writer.writerow.

At this point, the problem with the above code is solved.

2.pandasimport pandasa= {'sid': [7],' sname': ['hu'],' sage': [18], 'math': [100],' english': [90], 'cs': [85]} df = pandas.DataFrame (a) # mode =' a'is the appended data, and index is the index number of each row Header as the title df.to_csv ('supplier_data.csv',mode='a',index=False,header=False) on "python how to use pandas to read and write and append csv files" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out 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

Development

Wechat

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

12
Report