I would like to open a text file and prints its content
1 2 3 4 5 | >>> import os >>> os.chdir ("D:\python") >>> data = open("txt\AJokeADay1.txt") >>> for eachLine in data : print(eachLine) |
But it throw this exception:
1 2 3 4 | Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> for eachLine in data : UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 70: illegal multibyte sequence |
How To
Assign encoding to utf-8 as open this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> data = open("txt\AJokeADay1.txt", "r", encoding="utf-8") >>> for eachLine in data : print(eachLine) [In Office Jokes] A guy shows up late for work. The boss yells, ‘You should’ve been here at 8.30!’ He replies. ‘Why? What happened at 8.30?’ >>> data.close() |
No comments:
Post a Comment