亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能示例

 更新時間:2019年02月11日 11:14:14   作者:chengqiuming  
這篇文章主要介紹了Python實現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能,涉及Python大文件切分、讀取、多線程操作等相關(guān)使用技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)的大數(shù)據(jù)分析操作系統(tǒng)日志功能。分享給大家供大家參考,具體如下:

一 代碼

1、大文件切分

import os
import os.path
import time
def FileSplit(sourceFile, targetFolder):
  if not os.path.isfile(sourceFile):
    print(sourceFile, ' does not exist.')
    return
  if not os.path.isdir(targetFolder):
    os.mkdir(targetFolder)
  tempData = []
  number = 1000
  fileNum = 1
  linesRead = 0
  with open(sourceFile, 'r') as srcFile:
    dataLine = srcFile.readline().strip()
    while dataLine:
      for i in range(number):
        tempData.append(dataLine)
        dataLine = srcFile.readline()
        if not dataLine:
          break
      desFile = os.path.join(targetFolder, sourceFile[0:-4] + str(fileNum) + '.txt')
      with open(desFile, 'a+') as f:
        f.writelines(tempData)
      tempData = []
      fileNum = fileNum + 1
if __name__ == '__main__':
  #sourceFile = input('Input the source file to split:')
  #targetFolder = input('Input the target folder you want to place the split files:')
  sourceFile = 'test.txt'
  targetFolder = 'test'
  FileSplit(sourceFile, targetFolder)

2、Mapper代碼

import os
import re
import threading
import time
def Map(sourceFile):
  if not os.path.exists(sourceFile):
    print(sourceFile, ' does not exist.')
    return
  pattern = re.compile(r'[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}')
  result = {}
  with open(sourceFile, 'r') as srcFile:
    for dataLine in srcFile:
      r = pattern.findall(dataLine)
      if r:
        t = result.get(r[0], 0)
        t += 1
        result[r[0]] = t
  desFile = sourceFile[0:-4] + '_map.txt'
  with open(desFile, 'a+') as fp:
    for k, v in result.items():
      fp.write(k + ':' + str(v) + '\n')
if __name__ == '__main__':
  desFolder = 'test'
  files = os.listdir(desFolder)
  #如果不使用多線程,可以直接這樣寫
  '''for f in files:
    Map(desFolder + '\\' + f)'''
  #使用多線程
  def Main(i):
    Map(desFolder + '\\' + files[i])
  fileNumber = len(files)
  for i in range(fileNumber):
    t = threading.Thread(target = Main, args =(i,))
    t.start()

3.Reducer代碼

import os
def Reduce(sourceFolder, targetFile):
  if not os.path.isdir(sourceFolder):
    print(sourceFolder, ' does not exist.')
    return
  result = {}
  #Deal only with the mapped files
  allFiles = [sourceFolder+'\\'+f for f in os.listdir(sourceFolder) if f.endswith('_map.txt')]
  for f in allFiles:
    with open(f, 'r') as fp:
      for line in fp:
        line = line.strip()
        if not line:
          continue
        position = line.index(':')
        key = line[0:position]
        value = int(line[position + 1:])
        result[key] = result.get(key,0) + value
  with open(targetFile, 'w') as fp:
    for k,v in result.items():
      fp.write(k + ':' + str(v) + '\n')
if __name__ == '__main__':
  Reduce('test', 'test\\result.txt')

二 運行結(jié)果

依次運行上面3個程序,得到最終結(jié)果:

07/10/2013:4634
07/16/2013:51
08/15/2013:3958
07/11/2013:1
10/09/2013:733
12/11/2013:564
02/12/2014:4102
05/14/2014:737

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日志操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • 淺析python實現(xiàn)動態(tài)規(guī)劃背包問題

    淺析python實現(xiàn)動態(tài)規(guī)劃背包問題

    這篇文章主要介紹了python實現(xiàn)動態(tài)規(guī)劃背包問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Python中創(chuàng)建相關(guān)系數(shù)矩陣的方法小結(jié)

    Python中創(chuàng)建相關(guān)系數(shù)矩陣的方法小結(jié)

    相關(guān)系數(shù)矩陣是一種用于衡量變量之間關(guān)系的重要工具,本文將介紹在 Python 中創(chuàng)建相關(guān)系數(shù)矩陣的不同方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Python中對元組和列表按條件進(jìn)行排序的方法示例

    Python中對元組和列表按條件進(jìn)行排序的方法示例

    這篇文章主要介紹了Python中對元組和列表按條件進(jìn)行排序的方法示例,需要的朋友可以參考下
    2015-11-11
  • Python畫圖常用命令大全(詳解)

    Python畫圖常用命令大全(詳解)

    這篇文章主要介紹了Python畫圖常用命令大全,內(nèi)容包括,matplotlib庫默認(rèn)英文字體,讀取exal方法,論文圖片的類型和格式,柱狀圖擴展等知識,需要的朋友可以參考下
    2021-09-09
  • Python3的高階函數(shù)map,reduce,filter的示例詳解

    Python3的高階函數(shù)map,reduce,filter的示例詳解

    這篇文章主要介紹了Python3的高階函數(shù)map,reduce,filter的示例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • Python3 shelve對象持久存儲原理詳解

    Python3 shelve對象持久存儲原理詳解

    這篇文章主要介紹了Python3 shelve對象持久存儲原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • python中的循環(huán)語法使用指南

    python中的循環(huán)語法使用指南

    這篇文章主要給大家介紹了關(guān)于python中循環(huán)語法使用的相關(guān)資料, 循環(huán)語句是Python中的一種基本語句,用于重復(fù)執(zhí)行一段代碼。在Python中,循環(huán)語句分為for和while兩種,需要的朋友可以參考下
    2023-08-08
  • python中round函數(shù)如何使用

    python中round函數(shù)如何使用

    在本篇文章里小編給大家整理了關(guān)于python的round函數(shù)用法總結(jié)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • Python使用Windows API創(chuàng)建窗口示例【基于win32gui模塊】

    Python使用Windows API創(chuàng)建窗口示例【基于win32gui模塊】

    這篇文章主要介紹了Python使用Windows API創(chuàng)建窗口操作,結(jié)合實例形式分析了Python基于win32gui模塊調(diào)用Windows API創(chuàng)建窗口具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05
  • 在pytorch 中計算精度、回歸率、F1 score等指標(biāo)的實例

    在pytorch 中計算精度、回歸率、F1 score等指標(biāo)的實例

    今天小編就為大家分享一篇在pytorch 中計算精度、回歸率、F1 score等指標(biāo)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評論