第一则
"" "
批量给指定文件夹内的所有.csv文件增加表头后转成txt
"" "import osdef add_header_and_convert_to_txt( csv_folder, header, output_folder) :csv_files = [ f for f in os.listdir( csv_folder) if f.endswith( '.csv' ) ] for csv_file in csv_files:csv_file_path = os.path.join( csv_folder, csv_file) txt_file_name = os.path.splitext( csv_file) [ 0 ] + '.txt' txt_file_name = os.path.basename( txt_file_name) txt_file_path = os.path.join( output_folder, txt_file_name) with open( csv_file_path, 'r' , encoding = 'utf-8' ) as csv_file:lines = csv_file.readlines( ) lines.insert( 0 , header + '\n' ) with open( txt_file_path, 'w' , encoding = 'utf-8' ) as txt_file:txt_file.writelines( lines) print( '添加表头并转换为.txt文件完成。' )
csv_folder_path = 'your_folder_path'
header = 'column1,column2,column3,column4,column5,column6,column7,column8,column9,column10,column11,column12,column13,column14,column15,column16,column17,column18,column19,column20'
output_folder_path = 'your_output_folder_path' add_header_and_convert_to_txt( csv_folder_path, header, output_folder_path)
第二则
"" "
给文件夹内特定的.csv文件添加表头后转成txt
"" "
import osdef add_header_and_convert_to_txt( csv_folder, target_files, header) :for file_name in os.listdir( csv_folder) :file_path = os.path.join( csv_folder, file_name) if file_name.endswith( '.csv' ) and file_name in target_files:txt_file_path = os.path.splitext( file_path) [ 0 ] + '.txt' with open( file_path, 'r' , encoding = 'utf-8' ) as csv_file:lines = csv_file.readlines( ) lines.insert( 0 , header + '\n' ) with open( txt_file_path, 'w' , encoding = 'utf-8' ) as txt_file:txt_file.writelines( lines) print( '添加表头并转换为 .txt 文件完成。' )
csv_folder_path = 'your_folder_path'
target_csv_files = [ 'file1.csv' , 'file2.csv' ]
header = 'column1,column2,column3,column4,column5' add_header_and_convert_to_txt( csv_folder_path, target_csv_files, header)
第三则
"" "
根据特定日期,在文件夹内获取特定的.csv文件添加表头后转成txt
"" "
import os
import re
import datetimedef add_header_and_convert_to_txt( csv_folder, target_date, header) :pattern = re.compile( r'\d{8}\.csv$' ) for file_name in os.listdir( csv_folder) :if os.path.isfile( os.path.join( csv_folder, file_name)) :if pattern.match( file_name) :date_str = file_name.split( '.' ) [ 0 ] [ -8:] file_date = datetime.datetime.strptime( date_str, '%Y%m%d' ) .date( ) if file_date == target_date:csv_file_path = os.path.join( csv_folder, file_name) txt_file_path = os.path.splitext( csv_file_path) [ 0 ] + '.txt' with open( csv_file_path, 'r' , encoding = 'utf-8' ) as csv_file:lines = csv_file.readlines( ) lines.insert( 0 , header + '\n' ) with open( txt_file_path, 'w' , encoding = 'utf-8' ) as txt_file:txt_file.writelines( lines) print( f'已处理文件:{file_name}' ) print( '添加表头并转换为 .txt 文件完成。' )
csv_folder_path = 'your_folder_path'
target_date_str = '20230101'
header = 'column1,column2,column3,column4,column5' target_date = datetime.datetime.strptime( target_date_str, '%Y%m%d' ) .date( )
add_header_and_convert_to_txt( csv_folder_path, target_date, header)
for循环的两种不常见用法
for num in reversed( range( 0 , 31 )) :print( num) for num in range( 30 , 0 , -1) :print( num)