2015-01-07 1 views
-1

사용자 입력을 기반으로 특정 형식 (사용자 제안 사항의 도움)으로 텍스트 파일을 출력했습니다. 이제 "="기호의 오른쪽에 값만 해시하고 같은 형식으로 새 텍스트 파일을 출력하지만 오른쪽으로 값을 해시합니다."="기호 (또는 다른 구분 기호)의 오른쪽에 값만 해시하고 새 텍스트 파일을 출력하는 방법

import hashlib 

with open(outfh, 'r') as file: 

    lines = [x.strip('\n') for x in file.readlines()] 
    hashfile = 'C:\path_to_scripts\{}_hashed.csv'.format(device) 
    with open(hash_file,'w') as save_file: 


     # Write the saved file header 
     header = '\"Value\",\"Algorithm\",\"Hash\"\n' 
     save_file.write(header) 

     # For each line in file 
     for line in lines: 
      # Create a list of all the available algorithms 
      algorithms = ['md5','sha1','sha224','sha256','sha384','sha512'] 

      # For each algorithm 
      for algo in algorithms: 

       # Encode the original value as utf8 
       val_enc = line.encode('utf-8') 

       # Create the hashing object using the current algorithm 
       hashed = hashlib.new(algo) 

       # Set the value we want the hash of 
       hashed.update(val_enc) 

       # Get the hash 
       hash_digest = hashed.hexdigest() 


       results = '\"{}\",\"{}\",\"{}\"\n'.format(line, algo, hash_digest) 
       save_file.write(results) 

:

import csv 
 

 
device = input("Enter the device name: ").upper() 
 

 
output_file = 'C:\path_to_scripts\{}_items.txt'.format(device) 
 

 
with open(r'C:\path_to_script\filename_Brief.csv') as infh, \ 
 
    open(output_file, 'wt') as outfh: 
 
    reader = csv.DictReader(infh) 
 
    for row in reader: 
 
     if row['ALIAS'] == device: 
 
      outfh.write('Full_Name = {Full_Name}\n' 
 
         'PHONE_NO = {PHONE_NO}\n' 
 
         'ALIAS = {ALIAS}\n'.format(**row))

내가 같은 코드를 사용하여 전체 라인을 해시 할 수 있습니다 여기에 첫번째 부분 일 내 개조의 일부 나에게 제안 코드입니다 그러나 "Full_Name = Jack Flash"와 같이 해시 할 객체로 "Jack Flash"만 얻고, "Jack Flash"를 해시하고 키 형식으로 새 텍스트 파일에 값을 쓰는 등의 방법으로 줄을 분할하는 방법을 알아낼 수는 없습니다 = 해쉬 된 값. 위의 코드는 csv 파일에 저장됩니다. 나는 관련 섹션을 자르고 붙이기 때문에 의미가 있기를 바랍니다. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 미리 감사드립니다! 당신이 찾고있는 무엇

답변

0

는 str.split ('=') 당신이 해시 ''초기을하지 않으려면

>>> line = "Full_Name = Jack Flash" 
>>> parts = line.split('=') 
>>> parts 
['Full_Name ', ' Jack Flash'] 
>>> parts[1] 
' Jack Flash' 

, 제거합니다. 또는 '='가 * always '다음에' ', .split ('= ') 인 경우

관련 문제