当前位置:首页 > Python教程 > python技巧

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()
  

 

        

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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!