说我有一个功能:
def NewFunction():
return '£'
Run Code Online (Sandbox Code Playgroud)
我想在它前面打一些带有井号的东西,当我尝试运行这个程序时它会输出错误,显示以下错误信息:
SyntaxError: Non-ASCII character '\xa3' in file 'blah' but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在返回功能中加入英镑符号?我基本上是在课堂上使用它,它'__str__'包含在包含英镑符号的部分内.
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没有谈论编码,它只是谈论编码.
默认情况下,Python 3对源代码文件使用UTF-8编码.我还应该在每个源文件的开头使用编码声明吗?喜欢# -*- coding: utf-8 -*-
当我们#用于在Python中插入注释时,Python如何采用:
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
不同?
我如何在python中获得下面的字符数?
s = '??? ???? ???? ??? ????? ????.'
Char count: 29
Char length: 52
len(s) = 52
? = 29
Run Code Online (Sandbox Code Playgroud) 我写了一个程序来给我Mega Doge Coin的价值
import time
import urllib2
from datetime import datetime
def get_HTML():
response = urllib.request.urlopen('http://www.dogepay.com')
html = response.read()
return html
def get_Value(rawHTML):
index = rawHTML.find(“CCFF00”)
while(rawHTML[index] != “$”):
index = index + 1
index = index + 1
value = “”
while(rawHTML[index].isdigit() or rawHTML[index] == ‘.’):
value = value + rawHML[index]
index = index + 1
return float(value)
def get_DateTime():
now = datetime.now()
return '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,
now.minute, now.second)
def print_Output(DogeCoinValue, TimeDate):
print timeDate + …Run Code Online (Sandbox Code Playgroud) 最近,我一直在阅读Python源代码编码,尤其是PEP 263和PEP 3120.
我有以下代码:
# coding:utf-8
s = 'abc?´ƒ©'
ƒ = 'My name is'
ß = '?ß?ˆ†ˆ? ßå®åø©ˆ'
print('s =', s)
print('ƒ =', ƒ, 'ß =', ß)
Run Code Online (Sandbox Code Playgroud)
此代码适用于Python3,但SyntaxError在Python2.7中生成.
我知道这可能与源代码编码无关.
所以,我想知道是否有办法在Python2中支持Unicode变量名.
总而言之,我也很难弄清楚PEP究竟要解决的实际问题以及我如何(以及在何处)利用所提出的解决方案.我已经阅读了相同的讨论,但他们没有提出我的问题的答案,而是对正确语法的解释:
我在使用Python 2.5的谷歌应用引擎.我的应用程序必须处理多语言,所以我必须处理utf-8.
我做了很多谷歌,但没有得到我想要的.
1. # -*- coding: utf-8 -*-什么用途?
2.有什么区别
s=u'Witaj ?wiecie'
s='Witaj ?wiecie'
Run Code Online (Sandbox Code Playgroud)
'Witajświecie'是一首utf-8字符串.
3.当我将.py文件保存为'utf-8'时,我是否还需要u前面的每个字符串?
python ×8
encoding ×4
unicode ×3
python-2.7 ×2
utf-8 ×2
comments ×1
python-3.5 ×1
python-3.x ×1