with open(path) as f:
for line in f:
print line
path is file with single line as \xc2
when I run this I get
\xc2
now lets change this
with open(path) as f:
for line in f:
var = '\xc2'
print var
When I run this, I see
UnicodeDecodeError: 'ascii', '\xc2d', 0, 1, 'ordinal not in range(128)'
then I try to compare the so I do
with open(path) as f:
for line in f:
line = line.strip()
line1 = '\xc2d'
# print line1
print line == line1
and I see False
What is happening here??
When you read characters from a file,
\is just another character and has no special meaning. When you try to create a string from it though, it’s used as an escape that has special meanings depending on what follows. For example\xmeans take the next 2 hex digits and create the character that corresponds to the hex code, thus'\xc2'is a single character. Since this character code isn’t in the ASCII range of 0 to 127 (0x7f), you get an error when you try to print it.