Python character occurrences statistics (Counter package) and txt file write

# -*- coding: utf-8 -*-
#spyder (python 3.7)

1. Statistics character (can be used after jieba participle)

from Collections Import Counter
 from operator Import itemgetter 

# txt_list can be written as a function parameter is introduced 
txt_list = [ ' ages ' , ' human ' , ' human ' , ' long ' , ' long ' , ' long ' , ' ha ' , ' Talent ' , ' ages ' , ' ages ' ] 
C =Counter ()
 for X in txt_list:
     IF len (X)> =. 1 :
         IF X == ' \ R & lt \ n- '  or X == ' \ n- '  or X == '  ' :
             Continue 
        the else : 
            C [X] + . 1 =
 Print ( ' common word frequency statistics: \ n- ' )
 for (K, V) in c.most_common (. 4): # print top four 
    Print ( ' % S% S% S% D ' % ( '   '* (. 3), K, ' * ' *. 3 , V)) 

# according to the number printed word frequency descending 
D = the sorted (c.items (), itemgetter Key = (. 1), Reverse = True)
 for SS, TT in D: 
    out_words = SS + ' \ T ' + STR (TT)
     Print (out_words)

2. The multiple coverage, circulating written documents

# Write to the file, multiple writes, after a front cover once, but out_words itself is superimposed 
# namely: the first written is: eternal \ t3 \ n; the second is written: eternal \ T3 \ n the Long \ t3 \ n, a data coverage; 
# third time: ages \ T3 \ n Long \ T3 \ n Room \ t2 \ n, continues to overwrite the previous data 
out_words = '' 
for SS, TT in D: 
    out_words = out_words + SS + ' \ T ' + STR (TT) + ' \ n- ' 
    with Open (R & lt " . \ sss.txt " , " W " , encoding = ' UTF-. 8 ' ) AS F : 
        f.write (out_words + ' \ n- ' )

For example, the results of two cycles are:

 3. The one-time write to the file, and does not overwrite the middle repeatedly written; but if you repeatedly run the code, it will cover everything before one-time re-write all new content

out_words = ''
for ss,tt in d:
    out_words=out_words + ss + '\t' + str(tt) + '\n'
with open(r".\ttt.txt", "w",encoding='utf-8') as f:
        f.write(out_words+'\n')

Guess you like

Origin www.cnblogs.com/qi-yuan-008/p/11688911.html