here से कुछ मदद के साथ, मेरे पास यह लगभग वैसे ही काम कर रहा है जिस तरह से मैं चाहता हूं। अब की तुलना करने से पहले मुझे फ़ाइल से डेटा को हटाने की क्षमता जोड़ने की आवश्यकता है।फ़ाइल डेटा को प्रतिस्थापित करने के लिए रेगेक्स का उपयोग
इसका कारण तार है, "डेटा", जिसे मैं हटा रहा हूं, फ़ाइल को सहेजने के हर बार भिन्न होने के लिए जाना जाता है।
मैंने सटीक पाठ का चयन करने के लिए एक रेगेक्स लिखा है जिसे मैं निकालना चाहता हूं, लेकिन मुझे अपने वर्तमान कोड के साथ इसे लागू करने में समस्या हो रही है।
यहाँ तीन मुख्य कार्य
HOSTNAME_RE = re.compile(r'hostname +(\S+)')
def get_file_info_from_lines(filename, file_lines):
hostname = None
a_hash = hashlib.sha1()
for line in file_lines:
a_hash.update(line.encode('utf-8'))
match = HOSTNAME_RE.match(line)
if match:
hostname = match.group(1)
return hostname, filename, a_hash.hexdigest()
def get_file_info(filename):
if filename.endswith(('.cfg', '.startup', '.confg')):
with open(filename, "r+") as in_file:
#filename = re.sub(REMOVE_RE, subst, filename, 0, re.MULTILINE)
return get_file_info_from_lines(filename, in_file.readlines())
def hostname_parse(directory):
results = {}
i = 0
l = len(os.listdir(directory))
for filename in os.listdir(directory):
filename = os.path.join(directory, filename)
sleep(0.001)
i += 1
progress_bar(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
info = get_file_info(filename)
if info is not None:
results[info[0]] = info
return results
यह तार खोजने हटा दिया जाना चाहिए के लिए regex है कर रहे हैं।
REMOVE_RE = r"((?:\bCurrent configuration)(?:.*\n?){6})"
subst = ""
EXAMPLE_FILE_BEFORE_DATA_REMOVED:
Building configuration...
Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0
no service pad
!
no logging console
enable secret 5 ***encrypted password***
!
username admin privilege 15 password 7 ***encrypted password***
username sadmin privilege 15 secret 5 ***encrypted password***
aaa new-model
!
ip ftp username ***encrypted password***
ip ftp password 7 ***encrypted password***
ip ssh version 2
!
line con 0
password 7 ***encrypted password***
login authentication maint
line vty 0 4
password 7 ***encrypted password***
length 0
transport input ssh
line vty 5 15
password 7 ***encrypted password***
transport input ssh
!
EXAMPLE_FILE_AFTER_DATA_REMOVED:
Building configuration...
!
no service pad
!
no logging console
enable
!
username admin privilege 15
username gisadmin privilege 15
aaa new-model
!
ip ftp username cfgftp
ip ftp
ip ssh version 2
!
line con 0
login authentication maint
line vty 0 4
length 0
transport input ssh
line vty 5 15
transport input ssh
!
मैं #filename = re.sub (REMOVE_RE, subst, फ़ाइल नाम की तरह कुछ कर रही कोशिश की है, 0, re.MULTILINE) get_file_info के भीतर और get_file_info_from_lines b क्योंकि मैं स्पष्ट रूप से इसे सही ढंग से कार्यान्वित नहीं कर रहा हूं।
किसी भी मदद की सराहना की जाएगी क्योंकि मैं बस सीख रहा हूं।
तुलना चल रहा है:
results1 = hostname_parse('test1.txt')
results2 = hostname_parse('test2.txt')
for hostname, filename, filehash in results1.values():
if hostname in results2:
_, filename2, filehash2 = results2[hostname]
if filehash != filehash2:
print("%s has a change (%s, %s)" % (
hostname, filehash, filehash2))
print(filename)
print(filename2)
print()
मैं वर्तमान फ़ाइल को संशोधित करने के लिए नहीं करना चाहती। अगर यह सब स्मृति में किया जा सकता है या अस्थायी फ़ाइल बहुत अच्छी होगी।
पूर्ण कोड:
import hashlib
import os
import re
HOSTNAME_RE = re.compile(r'hostname +(\S+)')
REMOVE_RE = re.compile(r"((?:\bCurrent configuration)(?:.*\n?){6})")
def get_file_info_from_lines(filename, file_lines):
hostname = None
a_hash = hashlib.sha1()
for line in file_lines:
#match = HOSTNAME_RE.match(line)
if not re.match(REMOVE_RE, line):
a_hash.update(line.encode('utf-8'))
#=======================================================================
# if match:
# hostname = match.group(1)
#=======================================================================
return hostname, filename, a_hash.hexdigest()
def get_file_info(filename):
if filename.endswith(('.cfg', '.startup', '.confg')):
with open(filename, "r+") as in_file:
return get_file_info_from_lines(filename, in_file.readlines())
def hostname_parse(directory):
results = {}
for filename in os.listdir(directory):
filename = os.path.join(directory, filename)
info = get_file_info(filename)
if info is not None:
results[info[0]] = info
return results
results1 = hostname_parse('test1') #Directory of test files
results2 = hostname_parse('test2') #Directory of test files 2
for hostname, filename, filehash in results1.values():
if hostname in results2:
_, filename2, filehash2 = results2[hostname]
if filehash != filehash2:
print("%s has a change (%s, %s)" % (
hostname, filehash, filehash2))
print(filename)
print(filename2)
print()
कोई मदद? मैं अभी भी इसे सही तरीके से काम करने में असमर्थ हूं। – NineTail
क्या आप कृपया पूरा कोड जोड़ सकते हैं - ताकि हम जान सकें कि कौन सी libs का उपयोग किया जाता है? अपने कोड काम कर सकते हैं! क्षमा करें –
@ एमडी। सैफतुल इस्लाम हो गया। – NineTail