Python how to bulk read TXT file to dataframe format

Source: Internet
Author: User
Tags glob
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.