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 method of reading and storing Python data

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

Share

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

This article mainly introduces the Python data reading and storage methods of what is the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will get something after reading this Python data reading and storage method, let's take a look at it.

Csv file

1. Read csv file read_csv (file_path or buf,usecols,encoding): file_path: file path, usecols: specify the column name to be read, encoding: encoding

Data = pd.read_csv (data = pd.read_csv) data.head () name num0 buttered tea 219.01 highland barley wine 95.02 yoghurt 62.03 Ciba 16.04 pipa meat 2.0 # specified read column name Usecols= ['name']) data.head () name0 buttered tea 1 highland barley wine 2 yogurt 3 Zanba 4 pipa # if the file path is in Chinese Then you need to know the parameters engine='python'data = pd.read_csv ('dvv / data / food_rank.csv',engine='python',encoding='utf8') data.head () name num0 buttered tea 219.01 highland barley wine 95.02 yoghurt 62.03 Ciba 16.04 pipa meat 2.02.The suggested file path and file name should not appear in Chinese.

two。 Write to csv file

DataFrame:to_csv (file_path or buf,sep,columns,header,index,na_rep,mode): file_path: save the file path, default None,sep: delimiter, default',', columns: whether to retain a column of data, default None,header: whether to retain column names, default True,index: whether to retain row indexes, default True,na_rep: specify strings instead of null values, default is null characters, mode: default 'washes, append' a'

* * Series**: `Series.to _ csv` (_ path=None_,_index=True_,_sep='_,_'_,_na\ _ rep=''_,_header=False_,_mode='w'_,_encoding=None_) database interaction

Pandas

Sqlalchemy

Pymysql

# Import necessary module import pandas as pdfrom sqlalchemy import create_engine # initialize database connection # user name root password port 3306 database db2engine = create_engine ('mysql+pymysql://root:@localhost:3306/db2') # query statement sql =' 'select * from class '' # two parameters sql statement Database connection df = pd.read_sql (sql,engine) df# New df = pd.DataFrame ({'id': [1 Ling 2 Ling 3 Jing 4],' num': [3 4 Ling 5 6 Jing 78 J 90]}) df = pd.read_csv ('ex1.csv') # # write to database df.to_sql (' df2',engine) Index=False) print ("ok") on "what is the method of reading and storing Python data" is introduced here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the method of reading and storing Python data". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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