python 对文件几个操作尝试

f = open('C:/1.txt','r')
s = f.read()
u = s.decode('utf-8')
str = u.encode('utf-8')#转换为utf-8编码的字符串str
str1 = u.encode('gbk')#转换为gbk编码的字符串str1
 str1 = u.encode('utf-16')
print(s,"\n\n\nafter change as:\n\n")
print(s.lower())#upper lower



import re
import collections
print( collections.Counter( re.findall( '\w+' ,open( 'C:/1.txt' ).read( ) ) ) )



import collections
import re

patt = re.compile("\w+")
counter = collections.Counter(patt.findall(
    open('C:/1.txt', 'rt').read()
))



# top 10
for word, times in counter.most_common(20):
    print (word, times)



# find word
counter_dict = dict(counter.most_common(0))
tobefind = 'hello'
print (tobefind, counter_dict.get(tobefind, 0))


count what word
import re
txt =  open('C:/1.txt', 'rt').read()
print (len(re.findall("NBA", txt)))

你可能感兴趣的:(python)