Python - Ready text file and report count of individual characters -
i have python script prints out randomly generated password, prints prints text file, adding new row every time called. example:
pswd = (str(pswd01)) + (str(pswd02)) + (str(pswd03)) + (str(pswd04)) # note, variables pswd01, pswd02 etc randomly created earier in script. print(pswd) open('pswd_output.txt','a') f: f.write(pswd + '\n') f.close()
note, variable pswd contains lower case, upper case, , numbers.
i want read file , count number of individual characters, , print report different text file, not sure how can this. have asked similar question here, answers how print character report console.
any idea how can read pswd_output.txt, , count each different character, writing result separate text file?
any appreciated!
use dictionaries count characters repeat below:
my_dictionary = {} f = open('pswd_output.txt','r') line = f.readline() while(line): letter in line: if letter in my_dictionary: my_dictionary[letter] +=1 else: my_dictionary[letter] = 1 line = f.readline() print my_dictionary
for text file containing:
salam asjf asjg; asdkj 14kj'asdf 5
returns:
>>> ================================ restart ================================ >>> {'a': 6, 'j': 4, 'd': 2, 'g': 1, 'f': 2, 'k': 2, '\n': 6, 'm': 1, 'l': 1, "'": 1, '1': 1, 's': 5, '5': 1, '4': 1, ';': 1} >>>
Comments
Post a Comment