草庐IT

unicode-escapes

全部标签

python - 替换 Unicode 字符串中的换行符

我正在尝试替换unicode字符串中的换行符,但似乎缺少一些魔术代码。我的具体示例是,我正在使用AppEngine并尝试将HTML页面中的标题放入模型中的db.StringProperty()中。所以我做了类似的事情:link.title=unicode(page_title,"utf-8").replace('\n','').replace('\r','')我得到:Propertytitleisnotmulti-line我应该使用其他代码来替换吗? 最佳答案 尝试''.join(unicode(page_title,'utf-8'

python - 在python中将unicode代码点转换为UTF8 hex

我想将从文件中读取的一些unicode代码点转换为它们的UTF8编码。例如,我想将字符串'FD9B'转换为字符串'EFB69B'。我可以像这样使用字符串文字手动执行此操作:u'\uFD9B'.encode('utf-8')但我不知道如何以编程方式进行。 最佳答案 使用内置函数chr()将数字转换为字符,然后对其进行编码:>>>chr(int('fd9b',16)).encode('utf-8')'\xef\xb6\x9b'这是字符串本身。如果您希望字符串为ASCII十六进制,则需要使用hex(ord(c))或类似方法遍历并将每个字符

python - 我如何告诉 Python sys.argv 在 Unicode 中?

这是一个小程序:importsysf=sys.argv[1]printtype(f)printu"f=%s"%(f)这是我运行的程序:$pythonx.py'Recent/רשימתמשתתפים.LNK'Traceback(mostrecentcalllast):File"x.py",line5,inprintu"f=%s"%(f)UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xd7inposition7:ordinalnotinrange(128)$问题是sys.argv[1]认为它得到的是一个ascii字符串,它无法将其转换为Uni

python - json.loads() 返回一个 unicode 对象而不是一个字典

我正在使用fabric从远程服务器上的文件中读取json:fromStringIOimportStringIOoutput=StringIO()get(file_name,output)output=output.getvalue()output的值现在是:'"{\\n\\"status\\":\\"failed\\",\\n\\"reason\\":\\"Recordalready存在。\\"\\n}"'当我尝试使用json.loads(output)将此字符串解析为字典时,它返回unicode对象u'{\n"status":"failed",\n"reason":"Recordal

Python:为 unicode 清理字符串?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:PythonUnicodeDecodeError-AmImisunderstandingencode?我有一个字符串,我试图为unicode()函数确保安全:>>>s="foo“barbar”weasel">>>s.encode('utf-8','ignore')Traceback(mostrecentcalllast):File"",line1,ins.encode('utf-8','ignore')UnicodeDecodeError:'ascii'codeccan'tdecodebyte0x93inpo

Python:为 unicode 清理字符串?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:PythonUnicodeDecodeError-AmImisunderstandingencode?我有一个字符串,我试图为unicode()函数确保安全:>>>s="foo“barbar”weasel">>>s.encode('utf-8','ignore')Traceback(mostrecentcalllast):File"",line1,ins.encode('utf-8','ignore')UnicodeDecodeError:'ascii'codeccan'tdecodebyte0x93inpo

Python打印Unicode字符

我正在制作纸牌游戏,但遇到了似乎是编码问题的问题。我正在尝试打印这样的卡片:defprint(self):print("|-------|")print("|%s|"%self.value)print("||")print("|%s|"%self.suit.encode("utf-8"))print("||")print("|%s|"%self.value)print("|-------|")这就是我想要的:|-------||10||||♦||||10||-------|...但这就是我得到的:|-------||10||||b'\xe2\x99\xa6'||||10||------

Python打印Unicode字符

我正在制作纸牌游戏,但遇到了似乎是编码问题的问题。我正在尝试打印这样的卡片:defprint(self):print("|-------|")print("|%s|"%self.value)print("||")print("|%s|"%self.suit.encode("utf-8"))print("||")print("|%s|"%self.value)print("|-------|")这就是我想要的:|-------||10||||♦||||10||-------|...但这就是我得到的:|-------||10||||b'\xe2\x99\xa6'||||10||------

发现 Python 多处理 : TypeError: expected string or Unicode object, NoneType

我正在尝试并行下载整个ftp目录。#!/usr/bin/pythonimportsysimportdatetimeimportosfrommultiprocessingimportProcess,PoolfromftplibimportFTPcurYear=""remotePath=""localPath=""defdownloadFiles(remotePath,localPath):splitted=remotePath.split('/');host=splitted[2]path='/'+'/'.join(splitted[3:])ftp=FTP(host)ftp.login(

发现 Python 多处理 : TypeError: expected string or Unicode object, NoneType

我正在尝试并行下载整个ftp目录。#!/usr/bin/pythonimportsysimportdatetimeimportosfrommultiprocessingimportProcess,PoolfromftplibimportFTPcurYear=""remotePath=""localPath=""defdownloadFiles(remotePath,localPath):splitted=remotePath.split('/');host=splitted[2]path='/'+'/'.join(splitted[3:])ftp=FTP(host)ftp.login(