python将文件夹下csv文件转换成txt文件
程序员文章站
2022-09-21 09:50:21
python将文件夹下所有csv文件转换成所有txt,import pandas as pdimport os# 打开文件csv,转换一个文件def change_one(file_name): (shotname,extension) = os.path.splitext(file_name) data = pd.read_csv(file_path+file_name, encoding='utf-8')...
python将文件夹下所有csv文件转换成所有txt文件
# python ###############将文件夹下所有csv文件转换成所有txt################## import pandas as pd import os # 打开文件csv,转换一个文件 def change_one(file_name): (shotname,extension) = os.path.splitext(file_name) data = pd.read_csv(file_path+file_name, encoding='utf-8') # write_path = file_path+'txt_file\\' # if not os.path.exists(write_path): # os.makedirs(write_path) with open(file_path+shotname+'.txt','a+', encoding='utf-8',) as f: for line in data.values: f.write((str(line[0])+'\t'+str(line[1])+'\n')) #批量化 def change_all(file_path): listdir = os.listdir(file_path) for f in listdir: change_one(f) file_name = '地形数据.csv' file_path = 'E:\\James\\code\\data\\' change_all(file_path)