Jun-29-2021, 04:09 AM
Greetings!
I'm trying to sum(count) similar items in a file.
File example:
Code:
Counter({'PHQ': 4, 'ATC': 3, 'TCP_H': 2, 'DV1': 1, 'DG2': 1, 'TGP_H': 1})
How I can get (print to an output file) keys- values wihtuot the garbage?
Just this:
PHQ,4
ATC,3
TCP_H,2
DV1,1
DG2,1
TGP_H,1
Or maybe there is a better way to do this?
Thank you
I'm trying to sum(count) similar items in a file.
File example:
PRQ09_PCX0161Host,DV1 PRQ09_PCX0170Host,PHQ PRQ09_PCX0171Host,ATC PRQ09_PCX0173Host,ATC PRQ09_PCX0175Host,ATC PRQ09_PCX0176Host,PHQ PRQ09_PCX0179Host,DG2 PRQ09_PCX0180Host,TGP_H PRQ09_PCX0183Host,PHQ PRQ09_PCX0184Host,PHQ PRQ09_PCX0280Host,TCP_H PRQ09_PCX0380Host,TCP_HI thought I could use 'counter' and it does count very nice and produces the right output but I cannot access the Key-Value for some reason.
Code:
prdcs = [] with open (prod_BY_host, 'r') as pr_host : for lnf in pr_host : lnf=lnf.strip() host,prName = lnf.split(',') prdcs.append(prName) print(Counter(prdcs)) mout.write(str(Counter(prdcs))) mout.close()It prints :
Counter({'PHQ': 4, 'ATC': 3, 'TCP_H': 2, 'DV1': 1, 'DG2': 1, 'TGP_H': 1})
How I can get (print to an output file) keys- values wihtuot the garbage?
Just this:
PHQ,4
ATC,3
TCP_H,2
DV1,1
DG2,1
TGP_H,1
Or maybe there is a better way to do this?
Thank you