第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

求大佬幫幫忙,看一下這個(gè)關(guān)于python文本文件數(shù)據(jù)處理的問題?

求大佬幫幫忙,看一下這個(gè)關(guān)于python文本文件數(shù)據(jù)處理的問題?

catspeake 2021-08-25 20:15:01
請(qǐng)問:怎么把a(bǔ).txt中相同的set或者setenv合并起來(相同值忽略),若path1中含有mnt的換成${mntpath}且生成一行獨(dú)立的行,結(jié)果輸出到b.txta.txt:set value1 /usrset value1 /sysset value1 /sysset value2 /asdset value2 /xyzset value2 /xyzsetenv path1 /usr/lib:/usr/abcsetenv path1 /usr/asdsetenv path2 /usrsetenv path2 $path2:/abcsetenv path2 $path2:/aaasetenv path1 $path1:/mnt/abc:/mnt/xyzsetenv path1 $path1:/mnt/ccc:/mnt/dddb.txt:set value1 "/usr:/sys"set value2 "/asd:/xyz"setenv path1 "/usr/lib:/usr/abc:/usr/asd"setenv path2 "/usr:/abc:/aaa"setenv path1 "${path1}:/${mntpath}/abc:/${mntpath}/xyz:/${mntpath}/ccc:/${mntpath}/ddd"由于不知道怎么以code形式或以附件形式發(fā)布,所以只能這么貼了。
查看完整描述

3 回答

?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

#!/usr/bin/env python3# -*- coding: utf-8 -*- def zhidao_560604345(infile, outfile):    reader = open(infile, 'r')    set_dict = {}    setenv_dict = {}    while True:        line = reader.readline()        if len(line) == 0:            break        a, b, c = line.strip().split(maxsplit=2)        if == 'set':            if not in set_dict:                set_dict[b] = set()            set_dict[b].add(c.strip())        elif == 'setenv':            if not in setenv_dict:                setenv_dict[b] = set()            setenv_dict[b].update(c.strip().split(':'))    reader.close()    buff = []    for k, v in set_dict.items():        buff.append('set %s "%s"' % (k, ':'.join(list(v))))    for k, v in setenv_dict.items():        tmp = []        for item in list(v):            if item == '$' + k:                pass            elif item.startswith('/mnt/'):                tmp.append('{mntpath}/' + item[5:])            else:                tmp.append(item)        tmp.sort()        buff.append('setenv %s "%s"' % (k, ':'.join(tmp)))    writer = open(outfile, 'w')    writer.write('\n'.join(buff))    writer.close() if __name__ == '__main__':    zhidao_560604345('zhidao_560604345.input''zhidao_560604345.output')

運(yùn)行結(jié)果:


set value2 "/asd:/xyz"set value1 "/usr:/sys"setenv path2 "/aaa:/abc:/usr"setenv path1 "/usr/abc:/usr/asd:/usr/lib:{mntpath}/abc:{mntpath}/ccc:{mntpath}/ddd:{mntpath}/xyz"

 


查看完整回答
反對(duì) 回復(fù) 2021-08-30
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

  1. 分隔日志文件存為小文件

  2. #coding:utf-8 

  3. #file: FileSplit.py


  4. import os,os.path,time


  5. def FileSplit(sourceFile, targetFolder):

  6. sFile = open(sourceFile, 'r')

  7. number = 100000 #每個(gè)小文件中保存100000條數(shù)據(jù)

  8. dataLine = sFile.readline()

  9. tempData = [] #緩存列表

  10. fileNum = 1

  11. if not os.path.isdir(targetFolder):  #如果目標(biāo)目錄不存在,則創(chuàng)建

  12. os.mkdir(targetFolder)

  13. while dataLine: #有數(shù)據(jù)

  14. for row in range(number): 

  15. tempData.append(dataLine) #將一行數(shù)據(jù)添加到列表中

  16. dataLine = sFile.readline()

  17. if not dataLine :

  18. break

  19. tFilename = os.path.join(targetFolder,os.path.split(sourceFile)[1] + str(fileNum) + ".txt")

  20. tFile = open(tFilename, 'a+') #創(chuàng)建小文件

  21. tFile.writelines(tempData) #將列表保存到文件中

  22. tFile.close()  

  23. tempData = [] #清空緩存列表

  24. print(tFilename + " 創(chuàng)建于: " + str(time.ctime()))

  25. fileNum += 1 #文件編號(hào)


  26. sFile.close()


  27. if __name__ == "__main__" :

  28. FileSplit("access.log","access")

分類匯總小文件:

#coding:utf-8 

#file: Map.py

import os,os.path,re

def Map(sourceFile, targetFolder):

sFile = open(sourceFile, 'r')

dataLine = sFile.readline()

tempData = {} #緩存列表

if not os.path.isdir(targetFolder):  #如果目標(biāo)目錄不存在,則創(chuàng)建

os.mkdir(targetFolder)

while dataLine: #有數(shù)據(jù)

p_re = re.compile(r'(GET|POST)\s(.*?)\sHTTP/1.[01]',re.IGNORECASE) #用正則表達(dá)式解析數(shù)據(jù)

match = p_re.findall(dataLine)

if match:

visitUrl = match[0][1]

if visitUrl in tempData:

tempData[visitUrl] += 1

else:

tempData[visitUrl] = 1

dataLine = sFile.readline() #讀入下一行數(shù)據(jù)

sFile.close()

tList = []

for key,value in sorted(tempData.items(),key = lambda k:k[1],reverse = True):

tList.append(key + " " + str(value) + '\n')

tFilename = os.path.join(targetFolder,os.path.split(sourceFile)[1] + "_map.txt")

tFile = open(tFilename, 'a+') #創(chuàng)建小文件

tFile.writelines(tList) #將列表保存到文件中

tFile.close()

if __name__ == "__main__" :

Map("access\\access.log1.txt","access")

Map("access\\access.log2.txt","access")

Map("access\\access.log3.txt","access")

3. 再次將多個(gè)文件分類匯總為一個(gè)文件。

#coding:utf-8 

#file: Reduce.py

import os,os.path,re

def Reduce(sourceFolder, targetFile):

tempData = {} #緩存列表

p_re = re.compile(r'(.*?)(\d{1,}$)',re.IGNORECASE) #用正則表達(dá)式解析數(shù)據(jù)

for root,dirs,files in os.walk(sourceFolder):

for fil in files:

if fil.endswith('_map.txt'): #是reduce文件

sFile = open(os.path.abspath(os.path.join(root,fil)), 'r')

dataLine = sFile.readline()

while dataLine: #有數(shù)據(jù)

subdata = p_re.findall(dataLine) #用空格分割數(shù)據(jù)

#print(subdata[0][0],"  ",subdata[0][1])

if subdata[0][0] in tempData:

tempData[subdata[0][0]] += int(subdata[0][1])

else:

tempData[subdata[0][0]] = int(subdata[0][1])

dataLine = sFile.readline() #讀入下一行數(shù)據(jù)

sFile.close()

tList = []

for key,value in sorted(tempData.items(),key = lambda k:k[1],reverse = True):

tList.append(key + " " + str(value) + '\n')

tFilename = os.path.join(sourceFolder,targetFile + "_reduce.txt")

tFile = open(tFilename, 'a+') #創(chuàng)建小文件

tFile.writelines(tList) #將列表保存到文件中

tFile.close()

if __name__ == "__main__" :

Reduce("access","access")



查看完整回答
反對(duì) 回復(fù) 2021-08-30
  • 3 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)