Python批量将csv保存为txt

程序:

# _*_ coding: utf-8 _*_
__author__ = 'xbr'
__date__ = '2019/1/22 9:44'

import pandas as pd
import os

csvPath = r'F:\FY4A-AOD\satllite_base\PM10\result_ave'
outPath = r'F:\FY4A-AOD\satllite_base\PM10\result_ave_txt'
csvLists = os.listdir(csvPath)  # 列出文件夹下所有的目录与文件

for csvlist in csvLists:
    (filepath, fullname) = os.path.split(csvlist)  # 获取路径以及文件全名
    (prename, suffix) = os.path.splitext(fullname)  # 获取文件名(不含后缀)以及后缀名
    print prename
    df = pd.read_csv(csvPath+'\\'+csvlist)
    # 筛选列'col'和'row'数据,取值范围分别是(0,1538],(0,885]
    df = df[df['col'] > 0][df['col'] <= 1538][df['row'] > 0][df['row'] <= 885]
    df.loc[:, ['col', 'row', 'Predict']].to_csv(outPath+'\\'+prename+'.txt',
                                                header=None, sep=' ', index=None)

输入数据样例:

Python批量将csv保存为txt_第1张图片

程序输出数据样例:

Python批量将csv保存为txt_第2张图片

 

 

 

 

 

你可能感兴趣的:(Python批量将csv保存为txt)