如何在Python 2.7中打印非ASCII字符到文件 [英] How to print non-ascii characters to file in Python 2.7

查看:917
本文介绍了如何在Python 2.7中打印非ASCII字符到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过改变他们的字符代码来模糊化一些javascript,但是我发现在Python 2.7中,我无法正确地打印某个范围之外的字符。



例如,这里是我想要做的:

  f = open('text.txt' ,'w')
f.write(unichr(510).encode('utf-8'))
f.close()

我不能写unichr(510),因为它说ascii编解码器超出范围。所以我用utf-8编码。这会将单个字符 u'\\\Ǿ'变成两个'\xc7\xbe'。 p>

现在,在javascript中,很容易得到字符代码510的符号:

  String.fromCharCode(510)

给出单个字符:Ǿ



我用Python得到的是两个字符:



如果我将这些字符传递给javascript,原来的单个字符。



我知道可以在python中打印Ǿ字符,但我还没有能够弄清楚。我已经到使用unichr()而不是chr(),并编码为'utf-8',但我还是来了。我也读过Python 3有这个功能内置的chr()函数。但这不会帮助我。



有谁知道我如何完成这项任务?



谢谢。

解决方案

您应该以二进制模式打开该文件:

  f = open('text.txt','wb')

in Python 3):

  f.write(chr(510).encode('utf-8'))

或在Python 2中:

  f.write(unichr(510).encode('utf-8'))

最后,关闭文件

  f.close()

或者你可以这样做:

 >>>> f = open('e:\\text.txt','wt',encoding =utf-8)
>>> f.write(chr(510))
>>>> f.close()

之后,您可以读取该文件:

 >>> f = open('e:\\text.txt','rb')
>>> content = f.read()。decode('utf-8')
>>>>内容
'Ǿ'

 >>> f = open('e:\\text.txt','rt',encoding ='utf-8')
>>> f.read()
'Ǿ'

在我的Win7和Python3上测试。它应该与Python 2.X


I'm trying to obfuscate some javascript by altering their character codes, but I've found that I can't correctly print characters outside of a certain range, in Python 2.7.

For example, here's what I'm trying to do:

f = open('text.txt','w')
f.write(unichr(510).encode('utf-8'))
f.close()

I can't write unichr(510) because it says the ascii codec is out of range. So I encode it with utf-8. This turns a single character u'\u01fe' into two '\xc7\xbe'.

Now, in javascript, it's easy to get the symbol for the character code 510:

String.fromCharCode(510)

Gives the single character: Ǿ

What I'm getting with Python is two characters: Ǿ

If I pass those characters to javascript, I can't retrieve the original single character.

I know that it is possible to print the Ǿ character in python, but I haven't been able to figure it out. I've gotten as far as using unichr() instead of chr(), and encoding it to 'utf-8', but I'm still coming up short. I've also read that Python 3 has this functionality built-in to the chr() function. But that won't help me.

Does anyone know how I can accomplish this task?

Thank you.

解决方案

You should open the file in binary mode:

f = open('text.txt','wb')

And then write the bytes (in Python 3):

f.write(chr(510).encode('utf-8'))

Or in Python 2:

f.write(unichr(510).encode('utf-8'))

Finally, close the file

f.close()

Or you could do it in a better manner like this:

>>> f = open('e:\\text.txt','wt',encoding="utf-8")
>>> f.write(chr(510))
>>> f.close()

After that, you could read the file as:

>>> f = open('e:\\text.txt','rb')
>>> content = f.read().decode('utf-8')
>>> content
'Ǿ'

Or

>>> f = open('e:\\text.txt','rt',encoding='utf-8')
>>> f.read()
'Ǿ'

Tested on my Win7 and Python3. It should works with Python 2.X

这篇关于如何在Python 2.7中打印非ASCII字符到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆