程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python tutorial: there are three ways to read files: (read, readLine, readlines) detailed usage

編輯:Python

python3 in , There are three ways to read files :read()、readline()、readlines().

These three methods , Both support receiving a variable , Used to limit the amount of data read each time , however , It's not usually used .

Purpose of this paper : analysis 、 Summarize the usage and characteristics of the above three reading methods .

One 、read Method

  • characteristic : Read entire file , Put the contents of the file in a string variable .

  • shortcoming : If the file is very large , Especially when it is larger than memory , Can't use read() Method .

file = open(' Contact information of department colleagues .txt', 'r') # Create this file , Is an iterative object
try:
text = file.read() # The result is str type
print(type(text)) # Print text The type of
print(text)
finally:
file.close() # Close file file
"""
<class 'str'>
Li Fei 177 70 13888888
Wang Chao 170 50 13988888
Baijing 167 48 13324434
huangshan 166 46 13828382
"""

read() Read the bytes directly into the string , Including line breaks

>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.read()
>>> a
' Li Fei 177 70 13888888\n Wang Chao 170 50 13988888\n Baijing 167 48 13324434\n huangshan 166 46 13828382'

Two 、readline Method

  • characteristic :readline() Method reads one row at a time ; It returns a string object , Keep the memory of the current row

  • shortcoming : Than readlines It's much slower

file = open(' Contact information of department colleagues .txt', 'r')
try:
while True:
text_line = file.readline()
if text_line:
print(type(text_line), text_line)
else:
break
finally:
file.close()
"""
<class 'str'> Li Fei 177 70 13888888
<class 'str'> Wang Chao 170 50 13988888
<class 'str'> Baijing 167 48 13324434
<class 'str'> huangshan 166 46 13828382
"""

readline() Read entire line , Include line terminators , And return... As a string

>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.readline()
>>> a
' Li Fei 177 70 13888888\n'

3、 ... and 、readlines Method

characteristic : Read the entire file at once ; Automatically parse the contents of the file into a list of lines

'''
No one answers the problems encountered in learning ? Xiaobian created a Python Learning exchange group :711312441
Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book !
'''
file = open(' Contact information of department colleagues .txt', 'r')
try:
text_lines = file.readlines()
print(type(text_lines), text_lines)
for line in text_lines:
print(type(line), line)
finally:
file.close()
"""
<class 'list'> [' Li Fei 177 70 13888888\n', ' Wang Chao 170 50 13988888\n', ' Baijing 167 48 13324434\n', ' huangshan 166 46 13828382']
<class 'str'> Li Fei 177 70 13888888
<class 'str'> Wang Chao 170 50 13988888
<class 'str'> Baijing 167 48 13324434
<class 'str'> huangshan 166 46 13828382
"""

readlines() Read all lines , Then return them as a list of strings .

>>> file = open(' Part time model contact information .txt', 'r')
>>> a = file.readlines()
>>> a
[' Li Fei 177 70 13888888\n', ' Wang Chao 170 50 13988888\n', ' Baijing 167 48 13324434\n',
' huangshan 166 46 13828382']

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved