python txt文件批处理

首先,切换文件路径到所在文件夹

然后,将txt文件内容按行读取,写入到all.txt

def txtcombine():
    files=glob.glob('*.txt')
    all = codecs.open('all.txt','a')   #a代表追加
    for filename in files:
        print(filename)
        fopen=codecs.open(filename,'r',encoding='utf-8')
        lines=[]
        lines=fopen.readlines()
        fopen.close()
        for line in lines:
            for x in line:
                all.w

#读取为DataFrame格式 

all1 = pd.read_csv('all.txt',sep=' ',encoding='GB23
保存为csv文件
 #保存为csv格式
        all1.to_csv('all.csv',encoding='GB2312')
    return all
if __name__=='__main__':
    txtcombine()
  
 

猜你喜欢

转载自www.cnblogs.com/luban/p/9005037.html