I am trying to open and readlines a .txt file that contains a large amount of text. Below is my code, i dont know how to solve this problem. Any help would be very appreciated.

file = input("Please enter a .txt file: ")
myfile = open(file)
x = myfile.readlines()
print (x)

when i enter the .txt file this is the full error message is displayed below:

line 10, in <module> x = myfile.readlines()
line 26, in decode return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)
3

There are 3 answers

0
Alejandra Martínez On

@AndriiAbramamov is right, your shoud check that question, here is a way you can open your file which is also on that link

import codecs
f = codecs.open('words.txt', 'r', 'UTF-8')
for line in f:
    print(line)

Another way is to use regex, so when you open the file you can remove any special character like double quotes and so on.

0
Nicole Douglas On

You can also try to encode :

with open(file) as f:
    for line in f: 
         line = line.encode('ascii','ignore').decode('UTF-8','ignore')
         print(line)
0
yanzi1225627 On

Instead of using codecs, I solve it this way:

def test():
    path = './test.log'
    file = open(path, 'r+', encoding='utf-8')
    while True:
        lines = file.readlines()
        if not lines:
            break
        for line in lines:
            print(line)

You must give encoding param precisely.