码迷,mamicode.com
首页 > 编程语言 > 详细

python txt文件批处理

时间:2018-05-07 22:51:54      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:处理   highlight   lin   bin   odi   files   glob   coding   文件夹   

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

然后,将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()
  

 


python txt文件批处理

标签:处理   highlight   lin   bin   odi   files   glob   coding   文件夹   

原文地址:https://www.cnblogs.com/luban/p/9005037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!