Encode exception for chinese text
Hi all, I am new to python. I have written one small application which reads data from xml file and tries to encode data using apprpriate charset. I am facing problem while encoding one chinese paragraph with charset gb2312. code is: encoded_str = str_data.encode(gb2312) The type of str_data is type 'unicode' The exception is: UnicodeEncodeError: 'gb2312' codec can't encode character u'\xa0' in position 0: illegal multibyte sequence Can anyone please give me direction to solve this isssue. Regards, Vinayakc -- http://mail.python.org/mailman/listinfo/python-list
Re: Encode exception for chinese text
Yes serge, I have removed the first character but it is still giving encoding exception. -- http://mail.python.org/mailman/listinfo/python-list
Re: Encode exception for chinese text
Hey Serge, john, Thank you very much. I was really not aware of these facts. Anyways this is happening only for one in millions so I can ignore this for now. Thanks again, Vinayakc -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with exchange mail server
Hi Team, Thanks for reply. Yes, I can give you fair idea what I am doing here hdr = email.message_from_string( smtp_header_string ) tmpContentType = msg['Content-Type'] del msg['Content-Type'] for key in hdr.keys(): values = hdr.get_all(key) if values != None: if key not in msg: for value in values: msg.add_header(key,value) I have header for the mail in smtp_header_string. I want to generate RFC2822 file by using this. I found out solution for this. Header for which I am geeting failure has one line which is not valid header line. So by using following regular expression headerRE = re.compile(r'^(From |[\041-\071\073-\176]{2,}:|[\t ])') I parsed every line from header and if line doesn't satisfy this RE, I just dropped that line from header and then I passed this header string to email.message_from_string() function. Rest all is same. It works fine now. Thank you very much for your reply. Thanks and Regards Vinayakc -- http://mail.python.org/mailman/listinfo/python-list
Problem with exchange mail server
I am new for python. I have written one code which generates RFC2822 file. I have header in string format. i am using following code. emailData[SMTP_HEADER_POS] = emailData[SMTP_HEADER_POS].encode(utf-8) hdr = email.message_from_string(emailData[SMTP_HEADER_POS]) tmpContentType = msg['Content-Type'] del msg['Content-Type'] for key in hdr.keys(): values = hdr.get_all(key) if values != None: if key not in msg: for value in values: msg.add_header(key,value) if msg['Content-Type'] is None or msg['Content-Type'] == '': msg['Content-Type'] = tmpContentType is this right way to add the header?? This code is not doing well for exchange server mails. Python could not parse this mail header. Because first line is 'Microsoft exchange mail server version 2.0'. If I remove this line, it works. Could not understand this. Please help. Thanks and Regards Vinayakc -- http://mail.python.org/mailman/listinfo/python-list