0

When i write '你' in agend and save it as test-unicode.txt in unicode mode,open it with xxd g:\\test-unicode.txt ,i got :

0000000: fffe 604f                                ..`O

1.fffe stand for little endian
2.the unicode of is \x4f\x60

I want to write the as 604f or 4f60 in the file.

output=open("g://test-unicode.txt","wb")
str1="你"
output.write(str1)
output.close()

error:

TypeError: 'str' does not support the buffer interface

When i change it into the following ,there is no errror.

output=open("g://test-unicode.txt","wb")
str1="你"
output.write(str1.encode())
output.close()

when open it with xxd g:\\test-unicode.txt ,i got :

0000000: e4bd a0                                  ...

How can i write 604f or 4f60 into my file the same way as microsoft aengda do(save as unicode format)?

1
  • it is not good to take english as an example,i change it as chinese. Commented Apr 4, 2014 at 6:11

1 Answer 1

1

"Unicode" as an encoding is actually UTF-16LE.

with open("g:/test-unicode.txt", "w", encoding="utf-16le") as output:
  output.write(str1)
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried there is a bug in it ,it should be with open("g:/test-unicode.txt", "w", encoding="utf-16le") as output: not with open("g:/test-unicode.txt", "wb", encoding="utf-16le") as output:.
@it_is_a_literature: Right, fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.