简体   繁体   中英

Printing a txt file inside python shell

I have an assignment for class that has me transfer txt data from excel and execute in python. But every time I run it, only hex is displayed. I was wondering how to have the data displayed in ascii in the shell. This is the code I have so far. Is it possible to print it out in ascii in the shell?

infile = open("data.txt", 'r')
listName = [line.rstrip() for line in infile]
print (listName)
infile.close()

The reason its not working is because you are opening an Excel file - which is in a special format and is not a plain text file.

You can test this by yourself by opening the file in a text editor like Notepad; and you'll see the contents aren't in text.

To open the file and read its contents in Python you will need to do one of these two things:

  1. Open the file in Excel, then save it as a text file (or a comma separated file CSV). Keep in mind if you do this, then you can only save one sheet at a time.

  2. Use a module like pyexcel which will allow you to read the Excel file correctly in Python.

Just opening the file as plain text (or changing its extension) doesn't convert it.

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