Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird characters while reading file content

I'm not sure what is wrong:

for line in open(textfile, 'r'):
    print(line)

Output:

abcd

The file was created using textpad++ using Unix EOL and UTF8 encoding.

Now it works properly using Encoding with UTF-8 without BOM option on notepad++ . But why? I mean how could I convert all sent files to UTF-8 to avoid weird chars?

like image 223
thclpr Avatar asked May 28 '14 17:05

thclpr


1 Answers

Specifying encoding will solve your problem.

for line in open(textfile, 'r', encoding='utf-8-sig'):
    print(line)

utf_8_sig: UTF-8 codec with BOM signature

like image 158
falsetru Avatar answered Oct 14 '22 09:10

falsetru