javascript - js代码转python

查看:80
本文介绍了javascript - js代码转python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

js代码如下

var e = a.charCodeAt(t).toString(16);这一行不明白

function encode_unicode_param(a) {
    for (var s = "", t = 0; t < a.length; t++) {
        var e = a.charCodeAt(t).toString(16);
        s += 2 == e.length ? "n" + e: e
    }
    return s
}

解决方案

python3 下:

def encode_unicode_param(s):
    results = []
    for char in s:
        h = hex(ord(char))[2:]
        format_string = 'n{}' if len(h) == 2 else '{}'
        results.append(format_string.format(h))
    return ''.join(results)

这篇关于javascript - js代码转python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆