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