How to store ASCII encoded python string?

2006-08-28 Thread micahc
I currently have a Python program that reads in emails from a POP3
server.  As soon as the message is read in it is fed directly into a
PostgreSQL database for storage.  Later, it is broken down into it's
parts and displayed to the user.

My problem is that when I try to pass "\tsome text\xa7some more text\n"
into the database it gives me a unicode decode error.  At this point in
the program I don't know what codec it is (I won't know that until I
break apart the message later) and it may even be binary data so I just
want to store it in the database with the escape characters, not as a
decoded/encoded string.

So, how do I store "\tsome text\xa7 some more text\n" as that instead
of:
"   some text§ some more text
"

I don't have a problem escaping it so the above would look like
"\\tsome text\\xa7 some more text\\n" as long as I have a way to later
unescape it when I want to actual do something with the data.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to store ASCII encoded python string?

2006-08-28 Thread micahc
Fredrik Lundh wrote:
> 3) convert the data to Unicode before passing it to the database
> interface, and leave it to the interface to convert it to whatever
> encoding your database uses:
>
>  data = ... get encoded string from email ...
>  text = data.decode("iso-8859-1")
>  ... write text to database ...

Wouldn't that have to assume that all incoming data is in iso-8859-1?
If someone sends me an email with chinese characters would that still
work (I don't know the character set at data insert time)?


Marc 'BlackJack' Rintsch wrote:
> In [6]: '\tsome text\xa7some more text\n'.encode('string_escape')
> Out[6]: '\\tsome text\\xa7some more text\\n'

Thanks, I think this is what I will end up doing just for simplicity,
though I'm still curious about the above question.

-- 
http://mail.python.org/mailman/listinfo/python-list