Python文本处理几种方法
生活随笔
收集整理的這篇文章主要介紹了
Python文本处理几种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python文本處理幾種方法
方法一:readline函數
#-*-?coding:?UTF-8?-*- f?=?open("D:\pythontest\splm_ugslmd.log")????? line?=?f.readline() while?line:print(line,?end?=?'')line?=?f.readline() f.close()優點:節省內存,不需要一次性把數據讀取到內存中。
缺點:速度相對較慢。
方法二:一次讀取多行數據
#-*-?coding:?UTF-8?-*- f?=?open("D:\pythontest\splm_ugslmd.log") while?1:lines?=?f.readlines(10000)if?not?lines:breakfor?line?in?lines:print(line) f.close()優點:一次性把10000條數據讀取到內存中。
缺點:速度相對較快。
方法三:直接for循環
#-*-?coding:?UTF-8?-*- for?line?in?open("D:\pythontest\splm_ugslmd.log"):#print?line,??#python2print(line)
方法四:使用fileinput模塊
import?fileinput for?line?in?fileinput.input("D:\pythontest\splm_ugslmd.log"):print(line)
方法五:使用read讀取遠程服務器上的日志
轉載于:https://blog.51cto.com/hwg1227/2338611
總結
以上是生活随笔為你收集整理的Python文本处理几种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: parseInt和parseFloat(
- 下一篇: docker-machine creat