TA的每日心情 | 擦汗 2021-11-17 09:18 |
---|
签到天数: 79 天 [LV.6]常住居民II
|
发表于 2021-10-7 07:59:27
|
显示全部楼层
- C& y" e/ q3 z& A4 Y, \% q
好的!! 麻烦了1 ~3 r2 N( U. m& j" O
- h5 s* E! {+ k* @* |- import os i* H ^# L( x+ C8 u
- import glob. D% h2 g: O8 G( W
- import re
, d# i* e/ \8 t$ Q; l5 z9 n - import time
$ n' r% O6 C5 q' V- a7 H - # @( I+ u! W( J" {; Y1 k
- #遍历所有子文件
0 n* `& |# t8 E - def getFileListAll(filePath):
3 _) Y2 b O, k3 [3 [0 p - filelist=[]0 T" Q$ K y( G) H
- for root, dirnames, filenames in os.walk(filePath):% \: \. e: m9 v) ~
- for filename in filenames:5 a1 y4 n# Q2 b7 d' T7 R
- filelist.append(os.path.join(root,filename))
0 Y, [7 }6 I7 ^- j) {; X6 | - #print(os.path.join(root,filename)); b% A, ]4 ~3 ~7 H8 x z
- return filelist( w9 ^: L# G* _
- [& ^. I; ]/ J; v- #判断是否为txt文档$ h2 T6 s/ t3 Y, F
- def isTxts(nameList):- h, F% k+ e4 p- z$ U
- pattern=r"^[^~$].+\.(txt)$"
. P0 C5 d5 D7 Z7 A) e - patternObj=re.compile(pattern,re.I) \ f }6 I0 H( p) T
- notTxtList=[]( o( U \( P( j: X
- for fileName in nameList:
" I: c+ E0 F0 a- g+ d b: { - if not patternObj.fullmatch(fileName): #判断是否为txt文件 8 S! ]8 I5 u8 w+ B2 A& Z6 x
- notTxtList.append(nameList). P F9 C/ m' ]2 e% P1 x$ x) C
- if notTxtList:0 S9 d0 ?% I j& o8 c
- print("存在非txt文件:{0:},请处理后再运行程序!".format(notTxtList))+ X! C* i4 G. q' E# d/ @3 X
- return False
, _! s' x& A# g. x( N* X' t6 F- | - else:% A/ n' W4 \ _/ Y" Q& v/ [+ f
- return True ' w8 @; G- @: D/ }/ J1 a
7 a2 z6 y" _$ m& w- #在指定的txt文档中查找关键字
/ Y8 P8 v% q e9 I9 v - def readTxt(fileName):#strkey:查找的关键字;fileName:文件路径
# R: v g7 I4 ? - pattern=r"^[^~$].+\.(txt)$"
$ x* \3 o; o9 k% o - patternObj=re.compile(pattern,re.I) x% @, w4 \$ }7 h: W5 c) y7 T
- if patternObj.fullmatch(fileName): #判断是否为txt文件
' ^- j7 ?5 {7 L. C' n% q) o - try:
( |/ G/ Q% C! y, J& u8 M - f=open(fileName,"r",encoding="utf-8")1 V# K6 ?6 l. K2 P
- txt=f.read()
- z; W# Q" o" Q2 q+ t - f.close() 1 j8 X0 N) Z2 G% n6 m8 I
- except:: D( [& A0 u$ n* p. d0 |
- print("读取文档失败:{0:}".format(fileName))9 a8 v" |& c! u: f
- return "" 3 j' L5 F. P0 y" O8 M
- else: #无异常时,执行( W7 e* R/ I& S
- return txt
) e5 _9 S/ U+ Z - #finally: #无论是否异常,都执行8 p g3 e$ f5 D$ S
* H2 Q3 R# g- \5 n! G- def writeTxt(txt,outPath):' Y0 I ^* @6 o2 x7 m7 c* u
- path="\".join(outPath.split("\")[0:-1])* V! v) G I* {' c. e6 o6 d& e
- if not os.path.exists(path):4 N: r& f7 k" |9 k
- os.mkdir(path)
4 u7 N# _! ?- ~" O9 O - try:
* @$ @. n) b8 E" M( q' t - f=open(outPath,"a",encoding="utf-8")
' A- \+ j% b) k9 X1 s0 a6 Y' A& X - f.write(txt)" x4 v4 v; C5 O0 ^6 C
- f.close()+ |9 u9 b# [7 B1 D' l/ W5 g& B
- return True
5 j2 [& b: s8 G5 V - except:* w/ e5 w0 R; I6 [0 M8 P9 x2 y
- print("写入文档失败:{0:}".format(outPath)); }, i6 g" g: A- |" U
- return False& U0 F0 b! Q* T y
- #else: #无异常时,执行
8 B) o( S; Y3 C7 f& M3 s3 J6 E - # return txt
+ B4 V5 L: G& H) E; p7 I' _ - #finally: #无论是否异常,都执行
9 O+ H) t+ B4 {% P4 x
! x9 z! R+ }8 j% ]! c1 A& i2 p+ g- def getfileName(fileList):
, ^ }4 d, U- ^5 a - ''' u& ^$ G, J: C- y
- :param fileList:文件路径列表
/ e8 v1 ~2 ]) r4 l8 d6 Y# t; s - :return nameList:文件名称列表有扩展名
2 g0 D9 J5 s0 L' y9 K' | - '''
; D1 q9 \: H+ m! b* z - nameList=[]' @; c9 O; f" @0 T$ |- {3 y; H
- for fileName in fileList:
) p' @3 l% V6 M+ [: w& N - name=fileName.split("\")[-1]#提取文件名
|. ]/ b1 {- @. a y$ Z; G. M$ a - nameList.append(name)
( T! D" K) q9 E+ V: G U, \ - return nameList; ]0 B: t3 w& L
2 W- D' i: S6 L" c0 m- #显示重复的文件名,如果有重复文件显示重复的文件名称,并返回False,否则返回True
, }6 N! X9 Q" B; v: d/ c6 U" z - def showDupFile(nameList):
8 z& r: T5 @/ @+ D x - '''" [& H1 p5 i0 N9 |! J' B* S
- 判断是否有文件名相同的文件
! E% J: }* o0 c# r0 k0 q8 _3 g - :param fileList:文件名列表,包含绝对路径
- X/ U/ D y4 j# h& ?/ a! O0 U - :return: 如果有重复文件显示重复的文件名称,并返回False,否则返回True
3 u! K8 ~1 A, y e! Y: c# X0 x- K9 [ - '''
9 f7 }5 T+ M* a/ \8 U - if nameList: #如果文件名列表不为空( D3 n; s+ W$ F I8 L! c
- nameSet=set(nameList)+ {1 B- f+ I, \0 G5 ~
- DupNameList=[]) o; |9 [( _6 B+ Y
- for item in nameSet:) T2 r+ s% _7 A$ q# u
- if nameList.count(item)>1:
1 E+ u6 J: | _" m" i% `) m - DupNameList.append(item) u+ Y- E, W' B4 T! w9 H
- if DupNameList:#保存重复文件名的列表
8 V2 r8 R9 V9 H, I( T" c - for L in DupNameList:
) r7 l0 I: r4 x' A! x - print("{0:}为重复的文件,请处理!".format(L))
# _5 U2 D+ f5 h3 ^, L - return False; U8 x3 G2 [2 ^, N$ L/ t9 f1 Q$ m: y
- else:
, G, K3 c: D( L9 s5 V - print("没有发现文件名相同的文件,处理中...")
$ u3 I# V& u' R: l$ { - return True
3 o) y) \! |" f$ r
& C C {1 i; b M$ n! [+ d- def nameListSort(nameList,rev=True):, V5 i2 u* W; ?7 t% E/ Z2 A
- '''
1 ~& D4 M! y& ^2 G5 O0 p8 h, G( s t - 按照字符串长度排序
9 c+ ~ ^$ x0 W+ o+ Z, X' j - :param fileList:文件名列表" n0 R+ G" S: \" p
- :param reverse:默认降序5 L1 B- W, }1 X! e* K0 w
- :return newNameList:返回排序后的文件名新列表
2 G8 I2 T3 [8 W - '''' z- S) l `4 Y4 C J) J; I3 W
- newNameList = sorted(nameList,key = lambda i:len(i),reverse=rev) #按照字符串长度排序,降序
+ w1 T% m/ b" S: u6 ~) _ - return newNameList 6 Y; y' B: R6 O. H; N& o
- : B+ O# t3 V: H/ z; e6 k
- #添加超链接
" S0 d: g5 p: P - def formatTxtHref(nameList,txt):
k0 ^! K1 L* m% }; ^ - '''
% c }1 _' h: |' D! C - func:在文本中为特定字符串添加超链接
. [; E) i0 @6 p+ x7 P - :param hrefStr:待添加超链接的字符串
& a" S# Y, t$ \% J - :param txt:文本字符串
: X$ i5 L7 Z+ J; m2 m" E' i, o2 [- V - :return txt:格式化的文本字符串0 X; Y3 q4 Q$ M8 }+ A
- ''') {5 P3 n( }* @" o9 [
- print("\n开始循环添加超链接关键词辅助标记:")
: Q& Z+ b5 G) l* P( l* O. u - count=0/ \4 u. n6 L, b% w
- nameListDescend=nameListSort(nameList)
4 j2 T+ O* j4 S- |1 X$ ]/ r" S - for nameD in nameListDescend:3 v8 s# T8 `/ k1 o7 ]( y% I E
- nameDSimple=nameD.replace(".txt","")8 ?5 l/ K) u# c6 F8 r
- nameDSMarked="【@"+nameDSimple+"@】"5 {' {' d: L/ W2 F
- txt=txt.replace(nameDSimple,nameDSMarked)4 F, o. E1 o2 a v& k
- count=count+1/ c, j1 h$ W( u& T
- print("\r已完成第{0: ^6}个".format(count),end="") $ F5 o& ]4 Z' b; H, H- M
p* E1 m. [: q4 N- print("\n开始循环清理嵌套的冗余的超链接关键词辅助标记:")# j) T X& _4 T1 N, A( \
- count=0
0 N2 G1 A+ k6 E/ {5 V# W* x* ^ - nameListAscend=nameListSort(nameList,rev=False)
$ \- _7 |- t8 \: l, {( K - for nameA in nameListAscend:
' Q. l) h/ D8 S" i- [9 M% S - nameA=nameA.replace(".txt",""), H9 V+ B A4 ]. x* S2 Y
- pattern=r"【@([^@】]*?)【@{0:}@】".format(nameA) : ~' G3 s5 T+ r
- patternObjTxt=re.compile(pattern)
* S6 A5 P: d# A3 k$ k - toHrefStr="【@"+"\\1"+"muyubug"+nameA
! _$ W, t! t) ]( y' X8 U4 t* O0 M - txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
! T0 f. x2 l5 i - - S# s; Y4 u) B
- pattern=r"【@{0:}@】([^【@]*?)@】".format(nameA) 6 o- w! f# }* Q- c* t V4 w: t! [) r; q
- patternObjTxt=re.compile(pattern)6 r, L2 T- X( q0 l, e5 H+ B! s
- toHrefStr=nameA+"\\1"+"muyubug"+"@】"' z$ Y( A2 a% M" o- k" O1 n
- txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
4 D; ^4 s5 ?5 ^) o+ {1 D - txt=txt.replace("muyubug","")
: E1 A* F- M# L - count=count+1- I b: O. ]2 ]
- print("\r已完成第{0: ^6}个".format(count),end="")
1 `/ t4 a% R" h. v0 ] - " M8 x+ \2 r/ m0 n g; m& J, i7 I
- print("\n开始添加超链接,") - Y! ?. l: V1 p# Z
- pattern=r"【@([^【@]+?)@】"$ ]/ B% F8 F9 D! v8 V
- patternObjTxt=re.compile(pattern)
) ^* o; T, G6 X# b% C$ O1 X0 L - toHrefStr=r"<a href='entry://\1'>\1</a>"
$ O7 |' M% Z& q% s3 E0 _ - txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
, B* E& V% u9 ~0 ^ -
2 Y) x1 c+ ~& i8 S - print("开始清理辅助标记,")
4 h$ z- J" m: `. D, _, S - txt=txt.replace("<@>","")
9 Y9 |5 v% m: F+ W# f - return txt: U0 R4 G4 e: ~) o! z' ]+ B8 q# Y
- $ X& }: X" H% y" t( O: {
- def formatText(fileName):
0 Y/ u% W( b' R: W" U- e - '''& P8 P4 @0 _5 k, _
- func:清洗字符串,格式化
4 r2 X% U/ Y/ L1 \) E* T) N - :param fileName:待处理文件
2 w( b! v) `! A6 v( |: s" H - return txt:处理后的字符串1 }/ B* o" w2 ~: ]& v& J) J0 n
- '''9 p( ?: [2 N# ?& q7 u7 }
- #提取文件名
. F% `6 t9 t0 s. W - name=fileName.split("\")[-1].replace(".txt","")5 U& ?' R3 z& [; @& }
- #在文件名的每个字符键添加标记符" |+ |( ^& u- Z3 H( k/ Z
- pattern = re.compile('.{1,1}')
0 M+ I9 z( j2 b& D1 h- M+ l - matchCharList=pattern.findall(name)) k7 C7 _1 S* a0 d
- nameMaked="<@>".join(matchCharList)
7 S N9 ^5 I1 x - #生成标题部分- G; S8 E; l+ r% z/ b2 e! ~* c5 q
- txtTitle=nameMaked+"\n"
" D' f4 H4 Y( p& ^# v# q4 C - #读入文件
6 S$ N) P0 x% n& ^# E: A0 { - txtContent=readTxt(fileName) & ^; Y: _- F1 a) |, X$ P
- #替换正文中的特殊字符
8 m Y# }2 H4 ] C - strList=["\n","<br>"] q6 b, z$ J5 W7 }9 L! w3 @# i6 l% H
- for ch in strList:) @3 r- x8 o7 w9 ~6 w' ^' B3 ~5 s2 N
- txtContent=txtContent.replace(ch,"<BR>")
) Y7 m! U9 A7 U8 X V: ~& E! S4 p - #合成整个词条的内容
) Q0 _( H) ^" ?7 F8 H: G$ x - txt=txtTitle+txtContent+"<BR>\n</>"/ ?% k9 B4 I$ ?5 i( ~
- return txt i' l' N( q S# c
5 R$ d" n/ D; ]: H- def mdxFormat(path,outputPath):
. s2 e) @& X, r+ M. h - '''9 y% f# r+ @1 P
- func:格式化文本# n v! M! F! L* b9 e, l# x4 f7 G
- :param path:待格式化文本的路径4 j/ A" {, E+ x; x4 l
- :param outputPath:输出的路径和文件名4 X p8 I( u' |% g3 A3 }9 _" }4 X
- '''& u4 A8 k, B/ }! R" q# _ q% k5 J; ^
- fileList=getFileListAll(path)#获取指定目录下的全部文件,包括子目录中的文件8 J7 V" ]! M4 ^( a! G
- nameList=getfileName(fileList)
2 Y4 W2 b) F: u s+ W' @ - if isTxts(nameList):+ k6 \. o, B: J2 N! R$ Z2 T" _+ }
- if showDupFile(nameList):#如果没有重复文件+ r2 i9 F9 N. C5 D4 D
- print("找到了{0:}个txt文档,".format(len(nameList)))3 q, `) P/ u2 l0 L
' v9 X0 t6 J+ F, ^' }- print("开始格式化、合并文档:") . K& W7 U& U0 o
- count=0#txt文档数量统计
) U! z& I0 n3 S - txtList=[]#存储文件内容+ [0 i/ @( X+ A' S
- for fileName in fileList:
( H* k" @! U8 R& K - txt=formatText(fileName) #转换换行和<br>为<BR> ! f! t! r" c J2 ^
- txtList.append(txt)
2 T, `. ], _+ f$ \# g8 ?7 `$ d - count+=1
0 x8 N5 t) Q5 [3 |, P0 j& f - print("\r已完成第{0: ^6}个".format(count),end="") 4 V, ]) H) n; j6 j5 d0 C/ `8 d
- txt="\n".join(txtList)
$ k. A- d7 e2 O8 [. r - R0 h" ^7 Z( Z* @& Z. N& n3 A, b9 N9 Z
- #添加超链接) K# D4 D% \* P8 g! w- L* y
- txt=formatTxtHref(nameList,txt)- {( z6 x- ^8 b- V6 W9 ?( d
- print("开始写入文本,")
. G5 \. B& ^4 M! L - if writeTxt(txt,outputPath):/ G' f2 f2 U( T( t4 M# ?
- print("文件合并输出成功!")
+ x4 h& e8 l4 C! Y' Z& r' D - else:
5 v5 C+ j9 D; K4 B - print("Error:Merge!")( O- m8 A" U f) R: _" I
- return 1 {5 n) N1 o" I$ ~2 ^) O
- 2 M7 d. P' o: P+ p- o: }
- def main():0 K7 O; i( h5 \6 b7 k& j
- timeStart = time.time()
5 e4 U, F$ D6 ^- t - & z0 S# X% T7 d1 y ?) i9 u
* X4 `5 ^- L$ [; X1 s- E6 R' P' E" A' T, O
- path=r"/Users/vivian/Downloads/mdxFormat_upload_V3.5/test"
: x. }1 b( h$ N- L% m5 M -
. L; ^, z+ ^! G7 R - outputPath=r"/Users/vivian/Downloads/mdxFormat_upload_V3.5/output/demo.txt"
: ^2 k/ w8 t3 W" [5 l/ V0 E2 K - . q1 u5 ~8 s7 f: u2 j# F0 [
- mdxFormat(path,outputPath)% I% v0 @* `8 `5 }3 m9 `
- 3 T$ l6 }1 l8 L8 o) {2 d, G& _/ A
- timeEnd = time.time()& v# F; X2 I( N' P
- print("程序运行了%d秒"%(timeEnd-timeStart))( Z' _8 Z# R# h
" u: g& V1 ~# J! K5 O/ B- if __name__ == '__main__':- x( h* v7 ?# F- {
- main()- }3 u/ }/ f1 N. z; i1 _
复制代码 |
|