python txt file batch processing

First, switch the file path to the folder where it is located

Then, read the content of the txt file line by line and write it to 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

#Read as DataFrame format 

all1 = pd.read_csv('all.txt',sep=' ',encoding='GB23 
save as csv file
 #Save in csv format 
        all1.to_csv( ' all.csv ' ,encoding= ' GB2312 ' )
     return all
 if  __name__ == ' __main__ ' :
    txtcombine()
  

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325790110&siteId=291194637