如何打印文本文件的某些部分? Python

问题描述 投票:0回答:1
import os
import re 
from collections import Counter 
from collections import OrderedDict 
fileNames = []
textInfo = []
fileDict = {}

currentDirectoryPath = os.getcwd()
print(currentDirectoryPath)


regexp = re.compile(
    r'(?P<clientIP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+\['
    + '(?P<timestamp>\d{2}/[A-Z][a-z]{2}/\d\d\d\d).+\"'
    + '(?P<action>[A-Z]{3,4}).+\"'  
    + '\s*(?P<statuscode>[1-5][0-9][0-9])'
    )



os.chdir("/content/drive/log")
currentDirectoryPath = os.getcwd()
listOfFileNames = os.listdir(currentDirectoryPath)
#for files in listOfFileNames :
    #print(files) 


f = open('access_1.log', 'r')
matched = 0
failed = 0
cnt_clientIPs = Counter()
cnt_clientAction = Counter()
cnt_clientTimeStamp = Counter()
cnt_clientStatusCode = Counter()


for line in f:
    m = re.match(regexp, line)
    if m:
        cnt_clientIPs.update([m.group('clientIP')])
        cnt_clientAction.update([m.group('action')])
        cnt_clientStatusCode.update([m.group('statuscode')])
        matched += 1
    else:
        failed += 1
        continue

    print("""""\
client .........: %s
timestamp ......: %s
action .........: %s
statuscode.........: %s
""" % ( m.group('clientIP'),
        m.group('timestamp'),
        m.group('action'),
        m.group('statuscode'),
    ))




userInputIP = input("Enter how many of the top clients you want to see. ")
print('[*] %d lines matched the regular expression' % (matched))
print('[*] %d lines failed to match the regular expression' % (failed), end='\n\n')
print('[*] ============================================')
print('[*] '+ userInputIP +' Most Frequently Occurring Clients Queried')
print('[*] ============================================')

for clientIP, count in cnt_clientIPs.most_common(int(userInputIP)):
    print('[*] %30s: %d' % (clientIP, count))
print('[*] ============================================')



userInput=input("Would you like to see all clients from a certian date? (Yes or no)")
if userInput.lower() == "yes":
startdateInput = input("What date would you like to start at? ")
  enddateInput= input("What date would you like to end at? ")
  date_start = datetime.strptime(startdateInput, '%d/%b/%Y')
  date_end = datetime.strptime(enddateInput, '%d/%b/%Y')
  print('[*] ============================================')
  print('[*] '+ userInputIP +' Most Frequently Occurring Clients Queried')
  print('[*] ============================================')
  for clientIP, count in f:
    log_date = l.split()[3].lstrip("[").split(":")[0]
    log_date = datetime.strptime(log_date, '%d/%b/%Y')
    if date_start <= log_date <= date_end:
      print('[*] %30s: %d' %  (clientIP, count))

else:
  print('[*] ============================================')
  print('[*] '+ userInputIP +' Most Frequently Occurring Clients Queried')
  print('[*] ============================================')
  for clientIP, count in cnt_clientIPs.most_common(int(userInputIP)):
    print('[*] %30s: %d' % (clientIP, count))
print('[*] ============================================')

Enter how many of the top clients you want to see. 10
[*] 49997 lines matched the regular expression
[*] 3 lines failed to match the regular expression

[*] ============================================
[*] 10 Most Frequently Occurring Clients Queried
[*] ============================================
[*]                 205.167.170.15: 15695
[*]                  79.142.95.122: 3207
[*]                  52.22.118.215: 734
[*]                  84.112.161.41: 712
[*]                   37.1.206.196: 371
[*]                   91.200.12.22: 287
[*]                178.191.155.244: 284
[*]                 198.50.160.104: 249
[*]                   84.115.10.14: 234
[*]                  93.83.250.186: 219
[*] ============================================
Would you like to see all clients from all time or a certian date? (Yes or no)yes
What date would you like to start at? 05/feb/2016
What date would you like to end at? 10/feb/2016
[*] ============================================
[*] 10 Most Frequently Occurring Clients Queried
[*] ============================================
[*] ============================================

我正在从文件中读取内容,我想打印出用户输入的部分内容。我正在尝试从开始日期到结束日期。所有都在文件中给出。我想分开打印。这两个日期之间的ClientIP。


userInput=input("Would you like to see all clients from a certian date? (Yes or no)")
if userInput.lower() == "yes":
startdateInput = input("What date would you like to start at? ")
  enddateInput= input("What date would you like to end at? ")
  date_start = datetime.strptime(startdateInput, '%d/%b/%Y')
  date_end = datetime.strptime(enddateInput, '%d/%b/%Y')
  print('[*] ============================================')
  print('[*] '+ userInputIP +' Most Frequently Occurring Clients Queried')
  print('[*] ============================================')
  for clientIP, count in f:
    log_date = l.split()[3].lstrip("[").split(":")[0]
    log_date = datetime.strptime(log_date, '%d/%b/%Y')
    if date_start <= log_date <= date_end:
      print('[*] %30s: %d' %  (clientIP, count))

如您所见,我可以随时打印它们。因为这是文件中的所有内容。虽然我只希望它获得文件的特定部分以在那里打印日期。下面的文件中的文本示例。我想从2016年1月27日到2016年2月10日。因此,在下面提供的示例中,它将打印这4行客户端IP。188.23.144.118192.164.248.191192.164.248.191205.167.170.15

80.110.186.51 - - [21/Dec/2015:17:20:12 +0100] "GET /images/stories/raith/oststeiermark.png HTTP/1.1" 200 65225 "http://www.almhuette-raith.at/" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
188.23.144.118 - - [27/Jan/2016:20:50:42 +0100] "GET /images/phocagallery/Ferienwohnung_2/thumbs/phoca_thumb_m_2_wohnkche.jpg HTTP/1.1" 200 3141 "http://www.almhuette-raith.at/index.php?option=com_phocagallery&view=category&id=4:ferienwohnung2&Itemid=53" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586" "-"
192.164.248.191 - - [05/Feb/2016:23:53:03 +0100] "GET /images/phocagallery/almhuette/thumbs/phoca_thumb_m_jaegerzaun_gr.jpg HTTP/1.1" 200 5649 "http://www.almhuette-raith.at/index.php?option=com_phocagallery&view=category&id=1&Itemid=53" "Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-T530 Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.2 Chrome/38.0.2125.102 Safari/537.36" "-"
192.164.248.191 - - [05/Feb/2016:23:53:03 +0100] "GET /components/com_phocagallery/assets/js/shadowbox/src/skin/classic/skin.css HTTP/1.1" 200 5236 "http://www.almhuette-raith.at/index.php?option=com_phocagallery&view=category&id=1&Itemid=53" "Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-T530 Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.2 Chrome/38.0.2125.102 Safari/537.36" "-"
205.167.170.15 - - [10/Feb/2016:17:40:23 +0100] "GET /images/phocagallery/thumbs/phoca_thumb_l_winterfoto%209.jpg HTTP/1.1" 200 56144 "-" "Go-http-client/1.1" "-"
194.44.123.118 - - [18/Feb/2016:07:34:54 +0100] "GET /administrator/ HTTP/1.1" 200 4263 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36" "-"
python regex text-files
1个回答
0
投票
您可以使用:
© www.soinside.com 2019 - 2024. All rights reserved.