日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 处理日志文件

發布時間:2024/4/14 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 处理日志文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

對文本文件的處理,采用的是逐行處理的方法。Python代碼如下:

#coding:utf-8

import re
import os
import time
import sys
import json
file_object = open("abc.log","r")
output = open("abctemp.txt","w")
lines = file_object.readlines()

for line in lines:
? ? content_temp = line.split('{')
? ? if (len(content_temp)>1):
? ? ? ?contentfirst=content_temp[0].split(' ')
? ? ? ?if((contentfirst[0]>='I0615')):
? ? ? ? ? contentfirstsecond=contentfirst[1].split('.')
? ? ? ? ? if(contentfirstsecond[0]>='16:59:00'):
? ? ? ? ? ? ?content=content_temp[1].split('}')
? ? ? ? ? ? ?contentsecond=content[0].split(',')
? ? ? ? ? ? ?for temp in contentsecond:
? ? ? ? ? ? ? ? ?tempsecond = temp.split(':')
? ? ? ? ? ? ? ? ?if(tempsecond[0] == '"token"'):
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? output.write(tempsecond[1])
? ? ? ? ? ? ? ? ? ? output.write("\n")
output.close()
file_object.close()
print "\nsuccess"

這樣就取出了限制條件下指定的字段了。但是取出的信息存在重復的行,要進行去重處理。

代碼如下:

#coding:utf-8

import re
import time
import sys
from sets import Set

file("abcfinal.txt","w").writelines(Set(file("abc.txt","r").readlines()))

主要是利用Set中元素的不重復性,來達到去重的目的。先將源文件中的行取出寫到Set集合中,然后在將Set集合中的元素寫到目的文件中。

然后還有對兩個文本文件的內容的合并Python方法,代碼如下:

#coding:utf-8

import re
import time
import sys

fpa = open("e1.txt","r")
fpb = open("e2.txt","r")
fpc = open("e.txt","w")


arrB = []
for lineb in fpb.readlines():
? ? arrB.append(lineb)

index = 0
for linea in fpa.readlines():
? ? index = index+1
? ? fpc.write(linea)
? ? for i in range((index-1)*10,(index)*10):
? ? ? ? try:
? ? ? ? ? ? fpc.write(arrB[i])
? ? ? ? except:
? ? ? ? ? ? pass
print "Done!"

fpa.close()
fpb.close()
fpc.close()

這個是在網上找的,對range的范圍那個部分沒有看懂。接著再看看。

總結:

? ? ?Python對文本的處理還是很方便的。多多掌握,用來處理小事情還是挺方便的。

實習接到的第一個任務就是完成老大給的處理日志的任務。工作是JAVA實習生,聽說Python對文本處理的方便,我就硬著頭皮邊學Python邊處理任務了。順利完成。

轉載于:https://my.oschina.net/jaxiong/blog/731368

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的python 处理日志文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。