Reading backslashed characters (in ASCII) from a file

greg andruk meowing at banet.net
Thu May 27 10:42:42 EDT 1999


Dinu C Gherman <gherman at my-deja.com> writes:

> The problem I face now is that I want to convert these characters
> into their ASCII representation in a Python string. I know that
> Python does that itself, but the string is composed peu a peu and
> the upper example should look like this when splitted:

> ['s','p','a','m', ...]

> instead of:

> ['\\','1','6','3','\\', ...]

$ cat junk.py
import string
myfile = open('junk.txt', 'r')
thingy = string.rstrip(myfile.readline())
print "first try: ", thingy
print "second try: ", eval('"""' + thingy + '"""')

$ cat junk.txt
\163\160\141\155 and \145\147\147\163

$ python junk.py
first try:  \163\160\141\155 and \145\147\147\163
second try:  spam and eggs




More information about the Python-list mailing list