生活随笔
收集整理的這篇文章主要介紹了
CM资料下载操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
將apply_id寫入txt,每個txt存50條
def apply_id_txt ( l
, path
, channel
) : """l:列表,里面元素為字符串形式path:存放txt路徑channel:渠道:車貸、個貸...""" l_new
= [ ] for i
in l
: l_new
. append
( i
+ '\n' ) for i
in range ( np
. ceil
( len ( l
) / 50 ) . astype
( 'int' ) ) : l_s
= l_new
[ i
* 50 : ( i
+ 1 ) * 50 ] l_s
[ - 1 ] = l_s
[ - 1 ] . replace
( '\n' , '' ) with open ( os
. path
. join
( path
, '{}_{}.txt' . format ( channel
, ( i
+ 1 ) ) ) , 'w' ) as f
: f
. write
( 'LOAN_NUMBER\n' ) f
. writelines
( l_s
)
path
= r
'C:\Users\Administrator\Desktop\apply_id'
d
= pd
. read_excel
( r
'C:\Users\Administrator\Desktop\datamatch.xlsx' )
l
= d
[ 'apply_id' ] . astype
( '
str ) . tolist
( )
apply_id_txt
( l
, path
, '車貸' )
下載解壓后,去掉最外層文件夾
path
= r
'C:\Users\Administrator\Desktop\cm資料-學兵 - 備份'
for file in os
. listdir
( path
) : try : for sub_file
in os
. listdir
( os
. path
. join
( path
, file ) ) : shutil
. move
( os
. path
. join
( os
. path
. join
( path
, file ) , sub_file
) , os
. path
. join
( path
, sub_file
) ) shutil
. rmtree
( os
. path
. join
( path
, file ) ) except : print ( '錯誤' )
文件夾重命名+分組
def cm_rename ( path
, match_data
) : """path:存放cm文件夾路徑match_data:DataFrame形式,包含:apply_id,transport_id,mortgagor_name,dept_name,product_name,apply_submit_time等""" n
= [ ] for file in os
. listdir
( path
) : if len ( os
. listdir
( os
. path
. join
( path
, file ) ) ) == 0 : n
. append
( file )
dmap
= match_data
. copy
( ) dmap
[ 'concat' ] = dmap
. transport_id
. astype
( 'str' ) + "-" + dmap
. mortgagor_name
+ "-" + dmap
. dept_namename_match
= [ * zip ( dmap
. apply_id
. astype
( 'str' ) . tolist
( ) , dmap
. concat
. tolist
( ) ) ] for old_name
, new_name
in name_match
: try : os
. rename
( os
. path
. join
( path
, old_name
) , os
. path
. join
( path
, new_name
) ) except FileNotFoundError
as e
: print ( '重命名:' , e
) for file in os
. listdir
( path
) : if len ( os
. listdir
( os
. path
. join
( path
, file ) ) ) == 0 : shutil
. rmtree
( os
. path
. join
( path
, file ) ) for file in os
. listdir
( path
) : try : for png
in os
. listdir
( os
. path
. join
( os
. path
. join
( path
, file ) , 'image' ) ) : shutil
. move
( os
. path
. join
( os
. path
. join
( os
. path
. join
( path
, file ) , 'image' ) , png
) , os
. path
. join
( os
. path
. join
( path
, file ) , png
) ) shutil
. rmtree
( os
. path
. join
( os
. path
. join
( path
, file ) , 'image' ) ) except : print ( '將image文件下文件移到上一層文件夾錯誤' ) n_folder
= len ( os
. listdir
( path
) ) for file in os
. listdir
( path
) : name
= file . split
( '-' ) [ 3 ] if not os
. path
. exists
( os
. path
. join
( path
, name
) ) : os
. makedirs
( os
. path
. join
( path
, name
) ) shutil
. move
( os
. path
. join
( path
, file ) , os
. path
. join
( path
, name
) ) no_cm
= match_data
[ match_data
. apply_id
. isin
( n
) ] no_cm
. apply_id
= no_cm
. apply_id
. astype
( 'str' ) no_cm
. to_excel
( os
. path
. join
( path
, '空文件夾清單.xlsx' ) , encoding
= 'gbk' , index
= False ) return print ( '空文件:{}個\n有文件:{}個' . format ( len ( n
) , n_folder
) )
path
= r
'C:\Users\Administrator\Desktop\cm資料'
d
= pd
. read_excel
( r
'C:\Users\Administrator\Desktop\datamatch.xlsx' )
cm_rename
( path
, d
)
【參考資料】 1、Python中os包的用法 2、Python 文件夾重命名的兩種方法 3、Python 將列表數(shù)據(jù)寫入文件(txt, csv, excel)
總結(jié)
以上是生活随笔 為你收集整理的CM资料下载操作 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。