没有BOM的 UTF-8和UTF-8有什么不同?哪个更好?
我应该把shebang放在我的Python脚本中吗?以什么形式?
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
要么
#!/usr/local/bin/python
Run Code Online (Sandbox Code Playgroud)
这些同样便携吗?哪种形式最常用?
$ cat bla.py
u = unicode('d…')
s = u.encode('utf-8')
print s
$ python bla.py
File "bla.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file bla.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
如何在源代码中声明utf-8字符串?
示例代码:
>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud)
问题是:它不是人类可读的.我(智能)用户想要使用JSON转储验证甚至编辑文本文件.(我宁愿不使用XML)
有没有办法将对象序列化为utf-8 json字符串(而不是\ uXXXX)?
这没有帮助:
>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud)
这工作,但如果任何子对象是python-unicode而不是utf-8,它将转储垃圾:
>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud) 我收到了一些编码的文本,但我不知道使用了什么字符集.有没有办法使用Python确定文本文件的编码?如何检测文本文件的编码/代码页处理C#.
我正在写一些python代码,我收到标题中的错误消息,从搜索这与字符集有关.
这是导致错误的行
hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚ANSI ASCII集中没有哪个字符?此外,搜索"\ xe2"不再提供有关出现的字符的信息.该行中的哪个字符导致问题?
我也看到了一些针对这个问题的修复,但我不确定要使用哪个.有人可以澄清问题是什么(python不解释unicode,除非被告知这样做?),以及我如何正确清理它?
编辑:以下是错误附近的所有行
def createLoadBalancer():
conn = ELBConnection(creds.awsAccessKey, creds.awsSecretKey)
hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")
lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])
lb.configure_health_check(hc)
return lb
Run Code Online (Sandbox Code Playgroud) PEP 263定义了如何声明Python源代码编码.
通常,Python文件的前两行应该以:
#!/usr/bin/python
# -*- coding: <encoding name> -*-
Run Code Online (Sandbox Code Playgroud)
但我看到很多文件以:
#!/usr/bin/python
# -*- encoding: <encoding name> -*-
Run Code Online (Sandbox Code Playgroud)
=> 编码而不是编码.
那么声明文件编码的正确方法是什么?
是否允许编码,因为使用的正则表达式是懒惰的?或者它只是声明文件编码的另一种形式?
我问这个问题是因为PEP没有谈论编码,它只是谈论编码.
当我尝试在Windows控制台中打印Unicode字符串时,出现UnicodeEncodeError: 'charmap' codec can't encode character ....错误.我认为这是因为Windows控制台不接受仅Unicode字符.最好的方法是什么??在这种情况下,有什么方法可以让Python自动打印而不是失败?
编辑: 我正在使用Python 2.5.
注意: @ LasseV.Karlsen回答带有复选标记有点过时(从2008年开始).请谨慎使用下面的解决方案/答案/建议!!
截至今天(2016年1月6日),@ JFSebastian答案更为相关.
Python将以下内容识别为定义文件编码的指令:
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
我确实在(-*- var: value -*-)之前看过这种指令.它从何而来?什么是完整的规范,例如,值可以包括空格,特殊符号,换行符,甚至-*-本身?
我的程序将编写纯文本文件,我想使用这种格式在其中包含一些元数据.
我们已经在Python 2.6下运行了我们的代码库.为了准备Python 3.0,我们开始添加:
from __future__ import unicode_literals
进入我们的.py文件(因为我们修改它们).我想知道是否还有其他人这样做并遇到任何非显而易见的陷阱(也许是在花了很多时间调试之后).
python ×9
encoding ×4
unicode ×4
utf-8 ×3
emacs ×1
escaping ×1
file ×1
json ×1
python-2.6 ×1
python-3.x ×1
shebang ×1
shell ×1
text ×1
text-files ×1