TA的每日心情 | 擦汗 2021-11-17 09:18 |
---|
签到天数: 79 天 [LV.6]常住居民II
|
发表于 2021-10-7 07:59:27
|
显示全部楼层
$ @ W# r& @6 _7 O
好的!! 麻烦了
0 z$ m) x6 r7 m, f$ ?# \% a5 R, R/ l* a5 f: z
- import os& L, p2 }# _$ D1 k' t- S
- import glob& w6 c& ^' o, z% J. [3 N
- import re
0 _# m1 D. z2 v0 S - import time* S0 T1 q& S# x3 F- o4 D# B7 `
; X# ?* y2 w7 g4 x6 H4 ]- #遍历所有子文件
6 n U4 v7 c( e, U5 ] - def getFileListAll(filePath):) e% z) v: w; i( v; \
- filelist=[]+ ^+ i C' I3 l* Z$ Z) Y2 M
- for root, dirnames, filenames in os.walk(filePath):
/ e9 l4 @4 ^, v m0 V* ?3 J - for filename in filenames:
) c0 _ H7 m6 } J - filelist.append(os.path.join(root,filename))" F/ N4 F- U4 Q) L
- #print(os.path.join(root,filename))
* m) K$ e; z5 ?( w# } - return filelist
& _ y0 r9 V0 |0 Y7 e: i
' E0 v5 p* V/ c9 ^- #判断是否为txt文档( o/ O Y6 O& _: s2 P* Q/ R
- def isTxts(nameList):
8 s- y9 a1 W; ?- ^& ~& O, J - pattern=r"^[^~$].+\.(txt)$" % F/ J1 ?8 M$ [. D( M
- patternObj=re.compile(pattern,re.I)2 x# N& e, v% f' I- b6 v1 E
- notTxtList=[]$ y4 c, M. M, w! Z% n5 M0 X* J
- for fileName in nameList:
# g) `. y! }* H% ~1 L - if not patternObj.fullmatch(fileName): #判断是否为txt文件
5 }) j" U. P! B4 |( w; a, ^" _1 \" t - notTxtList.append(nameList)
8 S( d; C p T# ] - if notTxtList:* D/ ?' v1 p+ m
- print("存在非txt文件:{0:},请处理后再运行程序!".format(notTxtList))
. ?! I8 t0 F. {, a' e6 ~ - return False2 E, D4 }* L4 ~1 d! V- b7 O
- else: ?" C% y& {1 l' H% w
- return True
: ^( H" c7 r# L: x$ y* H# [; b) q - $ N; D0 e, ^3 W m
- #在指定的txt文档中查找关键字
. u8 U, s S; | - def readTxt(fileName):#strkey:查找的关键字;fileName:文件路径
. a4 a/ r, n6 L8 Y' @2 ^ - pattern=r"^[^~$].+\.(txt)$"
0 v8 c7 H4 ^$ D& t; K# s7 S( I - patternObj=re.compile(pattern,re.I)- m/ c/ F# h9 `, @
- if patternObj.fullmatch(fileName): #判断是否为txt文件 % g4 q4 x2 u# g0 [8 R) _0 {* B* }8 @. h
- try:
2 n/ D0 K- F& u6 n/ U3 V- V - f=open(fileName,"r",encoding="utf-8")0 X. W. `: h [9 j3 \
- txt=f.read()
4 U) u$ C: U: _' G( a! G% @0 @ - f.close() ( T' k! O7 D0 g$ z
- except:
/ S! `! x$ k* e, p7 G - print("读取文档失败:{0:}".format(fileName))- q* d4 J& {# |
- return "" ! L% d% M7 P( X4 b
- else: #无异常时,执行- I! ^$ p, m& p- T% f
- return txt
$ T; l) Q% ^8 E- e4 D6 F - #finally: #无论是否异常,都执行; a; t8 z0 y' J/ }; X* ?! G
) \$ Q$ g6 ^* i) D- def writeTxt(txt,outPath):
2 H! y* Z: `# r- Q; S4 @) W - path="\".join(outPath.split("\")[0:-1]). l$ j3 {$ ^" V9 o. ]9 H0 v! U
- if not os.path.exists(path):
; h4 F9 Z9 a6 m0 j- W# f - os.mkdir(path) $ g% ?6 l* P% q. {! \) q4 T' b7 T' i
- try:
1 S4 {7 ^2 R& d/ x6 | - f=open(outPath,"a",encoding="utf-8")1 C) f1 g: Q3 p1 P
- f.write(txt)6 C8 X }8 f% p( l1 ]9 J
- f.close()
. Q& P7 O% m! o) t - return True 7 ^, C1 E- |" Y8 p
- except:, F# M; I" Z4 g1 L$ y
- print("写入文档失败:{0:}".format(outPath))0 D6 [) y7 F; F( R& ^
- return False
) z/ q9 b3 Q2 g5 ~( W$ B P N - #else: #无异常时,执行
+ A4 b ?3 s1 L6 d$ W i - # return txt
' h" a' d3 J2 q! t' V - #finally: #无论是否异常,都执行
9 E, U4 t; W$ r) G - 1 M+ `2 k/ k/ ]+ J
- def getfileName(fileList):2 l E2 t. Y1 Z2 W: S1 J- q
- '''+ c6 T' g1 j6 A1 T# x& r
- :param fileList:文件路径列表) r( ~: Z# F% e. W e
- :return nameList:文件名称列表有扩展名! X% H T) T. ?3 S) B" N! X" k1 p
- '''
6 s9 g8 F* B* `9 N( o - nameList=[]
/ M5 i/ f9 r3 f: t" L - for fileName in fileList:
0 B9 J3 Q0 y+ N3 ] - name=fileName.split("\")[-1]#提取文件名
5 P2 p# }0 l. C, s - nameList.append(name)
/ r1 t* p4 M" Q) @% E - return nameList; R5 Y2 Q9 u3 a+ _8 Q" [; d8 G
- 7 k) K8 J- f5 C% O1 [- N8 b
- #显示重复的文件名,如果有重复文件显示重复的文件名称,并返回False,否则返回True4 ^3 z/ i! X8 _% {
- def showDupFile(nameList):4 W4 j3 U. Z' X
- '''0 L% M$ f* L; s
- 判断是否有文件名相同的文件 Q" V4 o0 I/ c5 y/ U9 b5 o! G
- :param fileList:文件名列表,包含绝对路径
( V `) K. F: A2 s" U - :return: 如果有重复文件显示重复的文件名称,并返回False,否则返回True5 ^# O( x: {8 V+ s+ J, \
- '''1 i6 ]: h# L' Q1 h8 ^
- if nameList: #如果文件名列表不为空9 g1 J# k9 p: Z
- nameSet=set(nameList)6 F! g1 @& ~# A4 J7 t
- DupNameList=[]
( ]. S3 p, i% I+ i5 Q7 ]- @2 E5 e - for item in nameSet:4 z) s. w! ?3 D& u
- if nameList.count(item)>1:
8 b8 h8 x u/ `5 j3 |( V - DupNameList.append(item)% L' `8 I1 _/ v N# V' j* B
- if DupNameList:#保存重复文件名的列表
, `6 W) J% Q% L" U - for L in DupNameList:4 V6 v6 F+ a1 M; e7 V9 `; ] ]
- print("{0:}为重复的文件,请处理!".format(L))
d y7 l+ @) k, r& }# { - return False
# t) v1 @. G. H7 [ }' A" u+ W - else: ?; Y0 g, C8 Q
- print("没有发现文件名相同的文件,处理中...")
' e f0 r8 O& ?1 v$ i* _ - return True
6 {" J2 k7 c8 @% v
+ g# y6 v$ {3 v; e1 ~( A" \- def nameListSort(nameList,rev=True):
% [3 g9 d# S0 B! H; { - '''% E/ H4 c, N A& i: |
- 按照字符串长度排序. d. I1 a6 D( Q/ [! ^ h
- :param fileList:文件名列表
, G q8 ^. f$ R! r - :param reverse:默认降序 {5 r+ I! V c( F
- :return newNameList:返回排序后的文件名新列表' | c& o4 H4 q. w* v6 x8 Y7 D6 Q: f
- '''
0 b; _0 B% V9 K - newNameList = sorted(nameList,key = lambda i:len(i),reverse=rev) #按照字符串长度排序,降序; J3 ]3 R- t' F' B, M# ]5 \
- return newNameList
& S; r$ |( a0 N" n+ ^
g1 V) C, v2 i" t4 H* a5 }- #添加超链接5 K5 x8 i m& @0 l" ? G! l. f6 l
- def formatTxtHref(nameList,txt):
& X$ o0 i/ o/ b* I x - '''
* J* H' x H3 Z1 m1 v4 n - func:在文本中为特定字符串添加超链接
! i4 h8 C9 ]2 M0 M9 I- A9 ?* | - :param hrefStr:待添加超链接的字符串8 }/ c/ b% s8 f, V9 ^$ x& g, g. K
- :param txt:文本字符串
% t8 O2 Q4 C! K& T- x - :return txt:格式化的文本字符串
3 N1 N, u1 c0 v% D - '''
' s/ _& G' j! G8 ~ - print("\n开始循环添加超链接关键词辅助标记:")! d- v# s, v. ^: G% A6 P
- count=0$ c( k2 }& _- \, B1 f+ A. K
- nameListDescend=nameListSort(nameList)$ q! `' }3 P3 ?5 A& Y
- for nameD in nameListDescend:
: K, f6 y7 S: e# a: E - nameDSimple=nameD.replace(".txt","")8 N$ n) l: C4 v/ b7 R; [; j5 h$ ^0 {
- nameDSMarked="【@"+nameDSimple+"@】"% K4 ~6 X+ a% C2 G k1 H p* [9 H
- txt=txt.replace(nameDSimple,nameDSMarked)7 h1 K: ^- V1 {" W; d8 ^& ^. N' P
- count=count+1
" l5 E* }) V% o! h" z z+ D% h - print("\r已完成第{0: ^6}个".format(count),end="") / J. E& m" u T. `
- 6 u5 _$ H4 ~, Q1 s: R6 m
- print("\n开始循环清理嵌套的冗余的超链接关键词辅助标记:")# O0 V& k; R$ I5 |/ G* @
- count=0- w3 F% r, G' |& M& m
- nameListAscend=nameListSort(nameList,rev=False)
1 K+ X$ J9 p+ E7 K - for nameA in nameListAscend:
0 l0 ?6 F$ e0 ]8 f( s4 t# \" F - nameA=nameA.replace(".txt",""). W+ l3 \' O9 v) t
- pattern=r"【@([^@】]*?)【@{0:}@】".format(nameA) $ x' Q) ]: G! C1 `
- patternObjTxt=re.compile(pattern)
; q7 c3 T/ b6 y5 O: E, b - toHrefStr="【@"+"\\1"+"muyubug"+nameA2 D1 H4 D% |) }& Q& y2 ?
- txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
9 a9 X1 t+ p! A! u& X6 u -
( _) i9 f, c$ |: P8 K ?0 {, \! n - pattern=r"【@{0:}@】([^【@]*?)@】".format(nameA)
+ O/ {; `% K' }$ b+ R - patternObjTxt=re.compile(pattern)7 T6 @% }% T; v1 y
- toHrefStr=nameA+"\\1"+"muyubug"+"@】"
( m/ W) E9 a& s/ x - txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
Y9 I0 c% |* w m6 u! J. L, M - txt=txt.replace("muyubug","")
" Z5 `- F% O2 I3 l' p6 m: f - count=count+1/ d- i- l8 j3 V& l
- print("\r已完成第{0: ^6}个".format(count),end="")
! O* p: \5 `* x, h' a$ }* r -
; I7 A- y' @" w+ w6 m4 e; d( T" s# S - print("\n开始添加超链接,") , \& k$ L8 w* V
- pattern=r"【@([^【@]+?)@】"
* ]! d1 M$ ^! ~ r: z - patternObjTxt=re.compile(pattern)/ J1 r T/ h" i7 d% h8 [
- toHrefStr=r"<a href='entry://\1'>\1</a>"5 U8 k) n' W; s4 O; F" a; w
- txt=patternObjTxt.sub(toHrefStr,txt,count=0) #替换为的字符串
- w9 s' t/ U8 n/ }. P* D - : [, H4 Y# Z6 ^! X3 t) O6 s) y
- print("开始清理辅助标记,") ) ^6 W- b& r# X, W3 Z. B
- txt=txt.replace("<@>","")& s7 Q" _' p& j$ R/ e+ q
- return txt
+ c5 o. c: R9 k7 k- T
. i2 o8 a% _# i% H( Y4 O, a- def formatText(fileName):. S$ a8 ?0 a: F. z7 R
- '''8 Z/ u7 G; O1 S8 g, `4 d
- func:清洗字符串,格式化4 H$ W, r% w1 N0 y g
- :param fileName:待处理文件! W1 v a& l$ Z+ R. ]1 Q R* w3 z
- return txt:处理后的字符串1 `; O# d: b1 D8 M1 W
- '''
! H7 _5 v: y0 G# ? - #提取文件名
$ b, L* g% c4 Z) G2 \ - name=fileName.split("\")[-1].replace(".txt","")
. ?( D! y9 ]1 a! o! `5 v - #在文件名的每个字符键添加标记符6 H; z. Y; }9 ]# _6 f' ~+ L3 U
- pattern = re.compile('.{1,1}'); @( Z3 K4 y1 a' b5 f
- matchCharList=pattern.findall(name)! `( y$ x0 P1 q1 d: A
- nameMaked="<@>".join(matchCharList)
$ D( y$ `; F( k - #生成标题部分3 f6 C% j6 W& Q4 p O
- txtTitle=nameMaked+"\n"
- f9 t% T# n% h - #读入文件$ ?: a1 t; N1 y, C+ m8 N
- txtContent=readTxt(fileName) # d% q2 c/ L3 \! S4 w& Z" G
- #替换正文中的特殊字符
+ S8 T$ ^$ o, E4 i" g" X7 a2 c, R - strList=["\n","<br>"]
% i( c- |+ X5 n* Z - for ch in strList:
8 J: J3 t G3 i+ g - txtContent=txtContent.replace(ch,"<BR>")
$ R m0 }( H6 e8 u# ]& G - #合成整个词条的内容5 Q* g9 f, I4 S, @% P" W5 T
- txt=txtTitle+txtContent+"<BR>\n</>"
~! [! E* W% O2 T - return txt; @6 T" n( l5 h8 |/ A j: F
% z+ e; n, }5 M3 _& u8 \. a. g- def mdxFormat(path,outputPath):
: X T: x. ^3 H - '''6 v- `8 E+ X6 z2 o* }+ f
- func:格式化文本
7 z: q b/ d& ? - :param path:待格式化文本的路径+ i5 _3 s" s+ g+ v* [7 r* j' [1 b
- :param outputPath:输出的路径和文件名" H5 ?- ~( x3 V$ x
- '''
, Z. ]9 Z: q3 t) z1 ^; a, P - fileList=getFileListAll(path)#获取指定目录下的全部文件,包括子目录中的文件) e' d7 h; y. k
- nameList=getfileName(fileList)
5 c1 s2 v7 @( p5 @2 v - if isTxts(nameList):* @. ~% Y, @& N, |. T; `2 F
- if showDupFile(nameList):#如果没有重复文件/ G' o& |( l+ u8 L- t$ ]: ^# A! w. [
- print("找到了{0:}个txt文档,".format(len(nameList)))
4 k; T/ m% \/ K
3 H' f0 E4 w& t- d$ q- print("开始格式化、合并文档:") : t1 f% y7 s; T3 u5 s4 l
- count=0#txt文档数量统计
; z: _; x" z$ z1 F9 a: Z% a9 b - txtList=[]#存储文件内容
+ k6 G+ w4 m8 D) r, A$ q - for fileName in fileList: ; M ?6 J$ m# d8 R8 f+ K- d. D) ~
- txt=formatText(fileName) #转换换行和<br>为<BR> ; c2 ^3 v0 G1 D+ w
- txtList.append(txt)# X2 }: g" e5 {0 c
- count+=17 x5 _) n' J6 a) G! M. t$ v
- print("\r已完成第{0: ^6}个".format(count),end="")
+ C6 W$ g! q% H5 S& h# ? - txt="\n".join(txtList)0 ]) ?- D$ v; D, D. E" ^8 [
- % l4 u% G( a4 ?" r' K% q
- #添加超链接; u/ L$ B6 `- i( i9 m: R+ r7 _9 V/ P
- txt=formatTxtHref(nameList,txt)
7 N- h5 o2 ^; t! O: l - print("开始写入文本,")
3 b4 R% p& H/ \7 j - if writeTxt(txt,outputPath):
4 f$ e8 r: K& F - print("文件合并输出成功!")/ g; j5 Q: r! D) S/ V
- else:4 ]- l( m% u0 s4 ]
- print("Error:Merge!")
! K) Y2 S( t% T, e$ C - return
0 q$ G. s0 }6 L -
2 }" N* S' P% L( Y# k: O- |4 g - def main():
2 m" ?" f1 \" r - timeStart = time.time()/ B: J; @7 n" h# B
h8 q# Q. g3 S' k- G/ a( p+ g0 f
- ( D+ y, s+ R. |0 r7 Y0 B7 F" H
- path=r"/Users/vivian/Downloads/mdxFormat_upload_V3.5/test"7 \) D; B7 L% z- Q
-
; ^7 o: b0 n6 p% a+ }8 k7 d - outputPath=r"/Users/vivian/Downloads/mdxFormat_upload_V3.5/output/demo.txt"
) X$ [& Y% C0 Y$ F" e2 A - 0 g4 D+ V* l* v9 Z9 A. X F$ L; M# _7 y, ~
- mdxFormat(path,outputPath)
" G+ D! O k% f8 w* X) v
2 x# X. v1 q; D- timeEnd = time.time()5 c9 Z4 q4 Y* b
- print("程序运行了%d秒"%(timeEnd-timeStart)); ]9 V6 p/ A4 w8 j
- 2 ~# m- c8 L+ J E1 X8 Z/ J. k5 O4 U+ }
- if __name__ == '__main__':
- g g( I' ^: l! N3 C3 q - main()
6 l/ h6 f7 x. ~+ T0 ~. j5 ?
复制代码 |
|