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 read Files with Python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to read files in Python. 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.

A Numpy library operation file 1. Manipulate the csv file import numpy as npa=np.random.randint (0jc10rep size = (3je 4)) np.savetext ("score.csv", a recorder delimi = ",")

A: a random array created by yourself, deliminter: delimiter, score: the file name to be read

Or

Import numpy as npdata=np.loadtxt ("score.csv", delimiter= ",", skiprows=1,dtype=str)

Skiprows: skip the first line, dtype: the type of data readout is character type

two。 Manipulate the csv file import csvwith open ("score.csv",'r') as fp:reader=csv.reader (fp) for x in reader: print (x) in pycharm

Reader: iterator

3. Other cases (.npy type files) import numpy as npc=np.random.randint (0Jing 10 memory size = (2jue 3)) np.save ("file name", c) c1=np.load ("file name .npy") two operation files in Pandas library 1. Manipulate the csv file import pandas as pddf=pd.read_csv ("exl.csv")

Or

Import pandas as pdpd.read_table ("exl.csv", sep=',')

Sep: delimiter

two。 Copy the data import pandas as pdBS=pd.read_clipboard3 from the clipboard. Read excel or xlsx file import pandas as pddf=read_excel ("exl.xlsx") III supplement 1. Commonly used import osos.chdir ()

Write the directory where you want to read the file in chdir (), which means change the directory to the directory where you want to read the file.

The function of reading files in 2.pandas

Read_csv loads delimited data from file, URL, file-type objects. The default delimiter is a comma

Read_table is the same as above, but the default delimiter is the tab ("t")

Read_fwf reads fixed width column format data (no delimiter)

Read_clipboard reads data from the clipboard

Read_excel reads tabular data from Excel or xlsx files

Read_hdf reads the HDF5 file written by pandas

Read_html reads all the tables in the html document

Read_json reads data in json string

Pandas data encoded in read_msgpack binary format

Read_pickle reads any object stored in python pickle format

Read_sas reads are stored on the SAS system in a custom storage format of SAS datasets

Read_sql reads DataFrame with SQL query result of pandas

Read_stata reads datasets in stata file format

This is the end of this article on "how to read files in Python". 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