This time to bring you python how to bulk read TXT file for dataframe format, Python bulk read txt file for the Dataframe format note what, the following is the actual case, take a look.
We sometimes process files in the same folder in batches, and we want to read a file that allows us to calculate the operation. For example, I have a series of txt files, how can I write them into a TXT file and read them in dataframe format?
First we need to use the Glob module, this python built-in module can be said to be very useful.
Glob.glob (' *.txt ')
The following results are obtained:
All.txt was the final document I got. You can see the return is a list containing the TXT file name, of course, if your folder is only TXT file, then you can use Os.listdir () to get a list of the same
Then read the time just note the TXT file encoding format (you can open Notepad with notepad++) and the form of the spacer, the complete code is as follows:
Import Osimport Pandasimport codecsimport Globimport Pandas as PDOS.GETCWD () os.chdir (' D:\AAAASXQ\python study\data Preprocessing ') def txtcombine (): files = Glob.glob (' *.txt ') all = Codecs.open (' All.txt ', ' a ') for FileName in flist: print (filename) fopen=codecs.open (filename, ' r ', encoding= ' utf-8 ') lines=[] Lines=fopen.readlines () fopen.close () i=0 for line in lines: for x in line : all.write (x) # Read as dataframe format all1 = pd.read_csv (' all.txt ', sep= ', encoding= ' GB2312 ') #保存为csv格式 all1.to_csv (' all.csv ', encoding= ' GB2312 ') If name = = ' main ': txtcombine ()
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How Python strings are converted to two-dimensional arrays
JS Eventemitter using step