在源代码中使用Unicode字符运行Python 2.7代码
我想运行一个Python源文件,该文件在源代码中包含unicode(utf-8)字符。我知道这可以通过在开头添加注释在源代码中使用Unicode字符运行Python 2.7代码,python,python-2.7,unicode,utf-8,Python,Python 2.7,Unicode,Utf 8,我想运行一个Python源文件,该文件在源代码中包含unicode(utf-8)字符。我知道这可以通过在开头添加注释#-*-coding:utf-8-*-来实现。不过,我希望不使用这种方法 我能想到的一种方法是以转义形式编写unicode字符串。比如说, 编辑:已更新源。添加了Unicode注释。 # Printing naïve and 男孩 def fxn(): print 'naïve' print '男孩' fxn() 变成 # Printing na\xc3\xafv
#-*-coding:utf-8-*-
来实现。不过,我希望不使用这种方法
我能想到的一种方法是以转义形式编写unicode字符串。比如说,
编辑:已更新源。添加了Unicode注释。
# Printing naïve and 男孩
def fxn():
print 'naïve'
print '男孩'
fxn()
变成
# Printing na\xc3\xafve and \xe7\x94\xb7\xe5\xad\xa9
def fxn():
print 'na\xc3\xafve'
print '\xe7\x94\xb7\xe5\xad\xa9'
fxn()
关于上述方法,我有两个问题
print u'nave'
打印u'长者'代码>
问题2:
如果你用键盘和中文输入软件输入句子,一切都应该正常。但是,如果你从一些网页中复制和粘贴句子,你应该考虑其他代码格式,如<代码> GBK< <代码>,<代码> GB23 12和<代码> GB18030< /Code > < P>此Python 3的代码段应该正确地将程序转换为Python 2中的工作。
def convertchar(char): #converts individual characters
if 32<=ord(char)<=126 or char=="\n": return char #if normal character, return it
h=hex(ord(char))[2:]
if ord(char)<256: #if unprintable ASCII
h=" "*(2-len(h))+h
return "\\x"+h
elif ord(char)<65536: #if short unicode
h=" "*(4-len(h))+h
return "\\u"+h
else: #if long unicode
h=" "*(8-len(h))+h
return "\\U"+h
def converttext(text): #converts a chunk of text
newtext=""
for char in text:
newtext+=convertchar(char)
return newtext
def convertfile(oldfilename,newfilename): #converts a file
oldfile=open(oldfilename,"r")
oldtext=oldfile.read()
oldfile.close()
newtext=converttext(oldtext)
newfile=open(newfilename,"w")
newfile.write(newtext)
newfile.close()
convertfile("FILE_TO_BE_CONVERTED","FILE_TO_STORE_OUTPUT")
def convertchar(char):#转换单个字符
如果32您的想法通常是合理的,但在Python3中会中断,并且在Python2中操作和编写字符串时会引起头痛
def convertchar(char): #converts individual characters
if 32<=ord(char)<=126 or char=="\n": return char #if normal character, return it
h=hex(ord(char))[2:]
if ord(char)<256: #if unprintable ASCII
h=" "*(2-len(h))+h
return "\\x"+h
elif ord(char)<65536: #if short unicode
h=" "*(4-len(h))+h
return "\\u"+h
else: #if long unicode
h=" "*(8-len(h))+h
return "\\U"+h
def converttext(text): #converts a chunk of text
newtext=""
for char in text:
newtext+=convertchar(char)
return newtext
def convertfile(oldfilename,newfilename): #converts a file
oldfile=open(oldfilename,"r")
oldtext=oldfile.read()
oldfile.close()
newtext=converttext(oldtext)
newfile=open(newfilename,"w")
newfile.write(newtext)
newfile.close()
convertfile("FILE_TO_BE_CONVERTED","FILE_TO_STORE_OUTPUT")
在处理非ASCII时,最好使用Unicode字符串,而不是常规字符串
相反,您可以将字符编码为Unicode字符串中的Unicode(不是UTF-8)转义序列
u'na\xefve'
u'\u7537\u5b69'
注意u
前缀
您的代码现在编码不可知。首先是一个简单的remarl:当您在Python2脚本中使用字节字符串时,#-*-编码:utf-8-*-
根本没有效果。仅当您编写了以下内容时,才有助于将源字节字符串转换为unicode字符串:
# -*- coding: utf-8 -*-
...
utxt = u'naïve' # source code is the bytestring `na\xc3\xafve'
# but utxt must become the unicode string u'na\xefve'
简单地说,聪明的编辑器可能会解释为自动使用utf8字符集
现在是实际问题。不幸的是,您所要求的并不是很简单:在源文件中定义注释和字符串中的内容只需要一个Python解析器。。。另外,如果您使用ast模块的解析器,您将丢失除docstring之外的注释
但在Python2中,非ASCII字符只允许出现在注释和普通字符串中!因此,您可以安全地假设,如果源文件是一个正确的Python 2脚本,不包含任何普通unicode字符串(*),那么您可以安全地转换其Python表示形式中的任何非ascii字符
从文件对象读取原始源文件并在编码后写入另一个文件对象的Python函数可能是:
def src_encode(infile, outfile):
while True:
c = infile.read(1)
if len(c) < 1: break # stop on end of file
if ord(c) > 127: # transform high characters
c = "\\x{:2x}".format(ord(c))
outfile.write(c)
def src_编码(填充、输出文件):
尽管如此:
c=填充读取(1)
如果len(c)<1:break#在文件末尾停止
如果ord(c)>127:#转换高位字符
c=“\\x{:2x}”。格式(ord(c))
输出文件写入(c)
一个很好的特性是,只要源文件是Python解释器可以接受的,并且不包含unicode literals(*)中的高位字符,并且转换后的文件的行为与原始文件完全相同,它就可以使用您使用的任何编码
(*)如果在Latin1以外的编码中使用unicode Literals,则会出现问题,因为上述函数的行为将类似于文件包含声明-*-编码:Latin1-*-
:u'é
将正确翻译为u'\xe9'
,如果原始编码是Latin1,则为u'\xc3\xc9'
(不是预期的…)如果原始编码是utf8,我无法想象在不完全解析源文件的情况下正确处理普通字节字符串和unicode字节字符串的方法…如果只使用字节字符串,并将源文件编码为UTF-8,则字节字符串将包含UTF-8编码的数据。无需编码语句(虽然你不想使用它真的很奇怪……这只是一个注释)。coding语句让Python知道源文件的编码,这样它就可以正确地解码Unicode字符串(u'xxxxx'
)。如果你没有Unicode字符串,那没关系
对于您的问题,无需转换为转义码。如果您将文件编码为UTF-8,则可以在字节字符串中使用可读性更强的字符
u'na\xefve'
u'\u7537\u5b69'
仅供参考,这对Python3不起作用,因为在该版本中字节字符串不能包含非ASCII
这就是说,这里有一些代码将根据请求转换示例。它读取源代码(假设源代码是以UTF-8编码的),然后使用正则表达式查找所有非ASCII字符。它通过转换函数将其传递以生成替换。这应该是安全的,因为非ASCII只能在字符串和文本中使用然而,Python2.Python3中的常量允许在变量名中使用非ASCII,因此这在那里不起作用
import io
import re
def escape(m):
char = m.group(0).encode('utf8')
return ''.join(r'\x{:02x}'.format(ord(b)) for b in char)
with io.open('sample.py',encoding='utf8') as f:
content = f.read()
new_content = re.sub(r'[^\x00-\x7f]',escape,content)
with io.open('sample_new.py','w',encoding='utf8') as f:
f.write(new_content)
结果:
# Printing na\xc3\xafve and \xe7\x94\xb7\xe5\xad\xa9
def fxn():
print 'na\xc3\xafve'
print '\xe7\x94\xb7\xe5\xad\xa9'
fxn()
好的。很抱歉我错过了这个。但是如果源代码也有unicode注释呢?我会更新这个问题。Python 3会解决你所有的问题。但是如果你必须使用Python 2,注释中的unicode会出错。我想你不能逃避#-*-编码:utf-8-*-
@Kingname:Python 3不会解决任何问题!这是一个输入编辑器问题,不是Python转换问题…@Kingnameprint u'长者'
仍需要指定编码-解释器需要知道如何将每个字符解码为Unicode字符。@Kingname复制和粘贴不受编码影响-您的OS/GUI负责将文本复制为中性编码或保留原始编码。UTF-8可用于所有已知字符,与GBK不同,GB2312 a