However, I did expect all three to have the same result (But they did not). I did not expect any of the three methods I outlined to be any more dynamic. Its not, I only did it this way to simply things. Perhaps if you specify more precisely what you mean by "dynamically created byte strings" and what exactly you're trying to do, an optimal solution can be found. You can build whatever you want out of those. :) If you want to work with Unicode, use the u'' format for the strings. Quote:Further investigation shows that the unicode object is intepreting the input string as a pure ASCII string and converting the characters to unicode characters.īecause that is exactly what you asked for. The difference here, compared to the first approach, is that here you create an ASCII string (as denoted by the lack of the 'u' prefix), put some escape characters into it that aren't valid for ASCII strings, and then convert it to Unicode, hoping the escape characters will now start working. In what way is this more dynamic than the first way? If I type the following in the idle editor: So either I've found alot of mis information, or I'm confused as heck :-) I've also tried the following with a similar result:įrom what I have read on several websites all three of the above should be equivalent. Further investigation shows that the unicode object is intepreting the input string as a pure ASCII string and converting the characters to unicode characters. Instead of printing the string out in a human readable format, it prints out the string '¿Habla español?'. If we manually allocate a new instance of the unicode object in the idle editor as follows: So we move onto a more dynamic way of generating the unicode object. Thats fine for constant strings, however I am dealing with dynamically created byte strings. Idle will print the spanish string in human readable format as expected. Therefore if I type the following in the idle editor: From what I read, python will by default encode these strings as utf-8. I've searched through various websites for my answer, and seem to have ended up with more questions then answers on the subject.įirst most, I understand that we can construct a new unicode object by prefixing any python string with an 'u'. The problem is not the network connection, but the generation of the unicode object. I'm in the process of trying to dynamically generate an python unicode object from an byte string that has been transmitted over an network connection.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |