How can eval() get the number of escape characters?

4 days ago 12
ARTICLE AD BOX

I'm not sure if this falls into troubleshooting/debugging as the code works, but I don't understand why it is working.

I started learning python a month or so ago and I decided to give Advent of Code a try, it was going ok but I got to day 8 where I had to count the amount of characters in a string which is pretty simple, but also count the amount of code characters in the string, that is including the quotes and escape characters, as the examples showed:

-"aaa\"aaa" is 10 characters of code, but the string itself contains six "a" characters and a single, escaped quote character, for a total of 7 characters in the string data. -"\x27" is 6 characters of code, but the string itself contains just one - an apostrophe ('), escaped using hexadecimal notation.

I had no idea how to do this because every way I tried, python would aways interpret "\x27" as "'", so after exhausting any ideas I decided to see what ways other people used to solve it and one thing that was often used was eval(), I tried to use it on Python Shell and it raised an error, however when I tried this code for some reason it returns the correct result:

Here is a partial of the input file

with open('day8-matchsticks.txt', 'r', encoding='utf-8') as file: lines = file.read().splitlines() def count_chars(lines): string_chars, code_chars = 0, 0 for line in lines: code_chars += len(eval(line)) string_chars += len(line) return string_chars - code_chars print(count_chars(lines))

I don't care about getting the exercise right if I don't understand it and in this case I still don't know why eval() works when inside the loop.

In the interactive shell if I try to pass the string "byc\x9dyxuafof\\\xa6uf\\axfozomj\\olh\x6a" to eval() it raises SyntaxError: invalid non-printable character U+009D and if I type '\\' in "byc\x9dyxuafof\\\xa6uf\\axfozomj\\olh\x6a" it returns False, but if I do that inside the loop it prints True for that same sentence, I don't understand what is happening here.

*Also, I forgot to add that printing line.count('\\') inside the loop returns 9 for that sentence meaning it counted all the backslashes, how can it do that if there are backslashes that are part of hexadecimal notation?

Read Entire Article