6

I have a text file that contains data like this. It is is just a small example, but the real one is pretty similar.

I am wondering how to display such data in an "Excel Table" like this using Python?

2

4 Answers 4

16

The pandas library is wonderful for reading csv files (which is the file content in the image you linked). You can read in a csv or a txt file using the pandas library and output this to excel in 3 simple lines.

import pandas as pd

df = pd.read_csv('input.csv') # if your file is comma separated

or if your file is tab delimited '\t':

df = pd.read_csv('input.csv', sep='\t')

To save to excel file add the following:

df.to_excel('output.xlsx', 'Sheet1')

complete code:

import pandas as pd
df = pd.read_csv('input.csv')
# can replace with:
# df = pd.read_csv('input.tsv', sep='\t') for tab delimited
df.to_excel('output.xlsx', 'Sheet1')

This will explicitly keep the index, so if your input file was:

A,B,C
1,2,3
4,5,6
7,8,9

Your output excel would look like this:

index excel example

You can see your data has been shifted one column and your index axis has been kept. If you do not want this index column (because you have not assigned your df an index so it has the arbitrary one provided by pandas):

df.to_excel('output.xlsx', 'Sheet1', index=False)

Your output will look like:

output excel no index

Here you can see the index has been dropped from the excel file.

Sign up to request clarification or add additional context in comments.

8 Comments

thank you for the knowledge. In the output file, the values seemed are not numbers, (Excel offers an option I clicked the cell "Convert to Number"). Any solution?
In my case, it print all the data in one column even after i added a comma in the text file.
@d_kennetz My values are storing in my text file in this way 0,0,0.0,0.0 and 1,1,1.0,1.0, and so on.
what is Sheet1?
@zixuan The name of your sheet. Make it whatever you want.
|
2

You do not need python! Just rename your text file to CSV and voila, you get your desired output :)

If you want to rename using python then - You can use os.rename function

os.rename(src, dst)

Where src is the source file and dst is the destination file

2 Comments

This solution is misguiding. At least with regard to the actual question. This is not a file conversion. This is a "hint" to excel, to open a text file.
Unfortunately, so many web sites use this as a crutch. The one that bothers me the most is PayPal - HUGE company and they can't seem to bother to provide true Excel files instead of CSV.
1

XLWT

I use the XLWT library. It produces native Excel files, which is much better than simply importing text files as CSV files. It is a bit of work, but provides most key Excel features, including setting column widths, cell colors, cell formatting, etc.

Comments

1

saving this is:

df.to_excel("testfile.xlsx")

2 Comments

It looks like that requires an external library (pandas?) - you need to specify that in your answer as to_excel is not a standard Python function.
you are so right. d_kennetz extended his reply. So this one is obsolete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.