遇到一个问题需要用Python把JavaScript中escape的中文给还原,但找了大半天,也没有找到答案,只好自己深入研究解决方案。
我们先来看在js中escape一段文字的编码
a = escape('这是一串文字');
alert(a);
输出:
%u8FD9%u662F%u4E00%u4E32%u6587%u5B57
咋一看,就感觉有点类似json格式,我们来看看标准的json格式编码同样的汉子“这是一串文字”
# encoding=utf-8
import json
a = '这是一串文字'
print json.dumps(a)
输出:
经过对比,其实就是js escape编码每个汉子都是“%u”符号加4位字符编码,而json编码每个汉子都是“\u”符号加4位字符编码,这样的话,我们可以利用字符串替换操作还原json格式,然后再使用json模块loads就好
# encoding=utf-8
import json
# js escape 字符串编码
c = '%u8FD9%u662F%u4E00%u4E32%u6587%u5B57'
# 还原Json对象
jsonObj = '"'+"".join([(i and "\\"+i) for i in c.split('%')])+'"'
print json.loads(jsonObj)
特别记得在把“%”替换为“\”符号以后还要再使用双引号把字符串包一下,才能算是一个json对象,然后才能json.loads出来
后来,好不容易在一个站点上看到了更简便的方法。代码如下:
# encoding=utf-8
c = '%u8FD9%u662F%u4E00%u4E32%u6587%u5B57'
print "".join([(len(i)>0 and unichr(int(i,16)) or "") for i in c.split('%u')])
它的思路其实都差不多,把“%u”号替换掉,剩下每一个都是4位固定长度的字符编码,最后在unichr反编码回中文字符。

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。