python txt文件批处理

Posted 我不是小鲁班

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
  

 



以上是关于python txt文件批处理的主要内容,如果未能解决你的问题,请参考以下文章