简体   繁体   中英

How to split the text file into rows and columns and save in excel using python

Screenshot of text file

I have a text file with a long sequence of data(combination of integer and string values) and I want to split the text file into rows and columns and then finally save the data into excel file using python.I tried to convert this file into csv but everything is getting stored into a single row.

enter image description here

I want the data to be splitted into different rows and columns based on the spacing. I tried with split function but its not working.

You can do this using pandas .

>>> import pandas as pd
>>> df = pd.read_table('filename.txt')
>>> df.to_excel('filename.xlsx')

In the read_table() method, adjust the delimiter method by using the sep= argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM