Python: Reading unicode data from server and writing to a file

Discussion in 'CGI/Perl/Python' started by thx_for_sharing, Nov 16, 2012.

  1. thx_for_sharing

    thx_for_sharing New Member

    I am fetching some strings from a server which have one utf-8 character. What I need to do is split on that UTF character and store the parts separately at 2 different places.
    For example: 詳細2.3

    The '' in the above string is nothing but U+F8FF unicode character (UTF8: EF A3 BF)

    I need to split the string on unicode character.
    Here is my code:
    text = open(r"C:\tempchar.txt").read()

    newpart = text.decode('utf-8').split(u"\uf8ff")
    firstpart = newpart[::2] #some manipulation on this later
    secondpart = newpart[1::2] #some manipulation on this later

    When I try this on a sample string from the text file as done in the code above, it works fine. And I can print the text on cmd prompt.

    But, when I do the same thing with the input string from a server, I get the following error:
    "UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)

    I have never seen this error before and have no idea what could be causing this. PLEASE HELP! :(

Share This Page