1

I was trying to read a txt file in python. After the following input:

f = open("test.txt","r") #opens file with name of "test.txt"
print(f.read(1))
print(f.read())

instead of looking at the text I'm returned this:

enter image description here

how do i visualize the output?

Thanks

Blue Moon
  • 4,421
  • 20
  • 52
  • 91

2 Answers2

0

I think you need to go line by line. Be careful, if its a big text file this could go on for awhile.

for line in f:
    print line.decode('utf-8').strip() 

some strings not being read correctly, so you'll need the decode line.

See:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

Community
  • 1
  • 1
AZhao
  • 13,617
  • 7
  • 31
  • 54
  • it doesn't work. I get the following error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128) – Blue Moon Jul 16 '15 at 19:48
  • can you share what your text file looks like? also take a look at my edited answer - see that error message's solutions – AZhao Jul 16 '15 at 19:50
0

try this setup... it worked for my console printing problems:

import sys
reload(sys)
sys.setdefaultencoding('utf8')
dot.Py
  • 5,007
  • 5
  • 31
  • 52