文本处理
"""
Spyder Editor
This is a temporary script file.
"""
import re
f= open ( 'd:/八十天环游地球.txt' , 'r' )
f2= open ( 'd:/八十天环游地球new.txt' , 'w' )
f3= open ( 'd:/八十天环游地球-章节.txt' , 'w' )
for i in f:
i= i. rstrip( )
i= re. sub( '<\/?.*>' , '' , i)
i= re. sub( '(")' , '\"' , i)
i= re. sub( '(99lib.t)' , '' , i)
if len ( i. strip( ) ) > 0 :
f2. write( i+ '\n' )
j= i. strip( )
if j. split( ) [ 0 ] [ 0 ] == '第' and '章' in j. split( ) [ 0 ] :
f3. write( j+ '\n' )
f2. close( )
f3. close( )
f. close( )
"""
Created on Tue Dec 21 22:33:33 2021
@author: 焜曦
"""
import jieba
f= open ( "D:/Desktop/Python作业/文本处理/80天环游地球/八十天环游地球(处理).txt" , 'r' )
datas= f. readlines( )
l= [ ]
for i in range ( len ( datas) ) :
data= datas[ i] . strip( )
if len ( data) > 0 :
ll= data. split( ) [ 0 ]
if ll[ 0 ] == '第' and '章' in ll:
l. append( i)
stopWords= [ '先生' , '他们' , '福克' , '路路通' , '没有' , '克斯' ]
for i in range ( len ( l) ) :
if i!= len ( l) - 1 :
data= datas[ l[ i] + 1 : l[ i+ 1 ] - 1 ]
else :
data= datas[ l[ i] + 1 : ]
data= '' . join( data)
words= jieba. cut( data)
fre= { }
for w in words:
if len ( w) < 2 or w in stopWords:
continue
else :
fre[ w] = fre. get( w, 0 ) + 1
lis= list ( fre. items( ) )
lis. sort( key= lambda x: x[ 1 ] , reverse= True )
print ( datas[ l[ i] ] . strip( ) . split( ) [ 0 ] , lis[ 0 ] [ 0 ] , lis[ 0 ] [ 1 ] )
f. close( )
上面的代码是上课老师写的,初学python 加了点注释 代码效率可能不算高效,记录一下