JavaScript 到 Python

问题描述

我是 Python 新手。目前我正在做一个项目,我想要一个可以从文本中提取数字的代码。我在互联网上找到了一个代码,但它是用 java 脚本编写的。我想在 python 中运行它,但不知道该怎么做。我尝试了 js2py,但它抛出了一个支持 ECMA 5.1 的异常,但此代码是更高版本。有人可以指导我如何在 python 中运行这段代码。或者谁能​​把这段代码转换成python代码或者ECMA 5.1代码

const PHONE_REGEXS_STRINGS = [
    // 775123456
    '[0-9]{6,15}',// 1(413)555-2378 or 1(413)555.2378 or 1 (413) 555-2378 or 1 (413) 555 2378 or (303) 494-2320
    '([0-9]{1,4}( )?)?\\([0-9]{2,4}\\)( )?[0-9]{2,4}(( )?(-|.))?( )?[0-9]{2,6}',// 1(262) 955-95-79 or 1(262)955.95.79
    '([0-9]{1,// 413-577-1234-564
    '[0-9]{2,4}-[0-9]{2,// 413-577-1234
    '[0-9]{2,// 413-577
    '[0-9]{2,// 413.577.1234.564
    '[0-9]{2,4}\\.[0-9]{2,// 413.577.1234
    '[0-9]{2,// 413.577
    '[0-9]{2,// 413 577 1234 564
    '[0-9]{2,4} [0-9]{2,// 413 577 1234
    '[0-9]{2,// 123 4567
    '[0-9]{2,4} [0-9]{3,8}',];

// All phones might be prefixed with '+' or '00'
for (let i = 0; i < PHONE_REGEXS_STRINGS.length; i++) {
    PHONE_REGEXS_STRINGS[i] = `(00|\\+)?${PHONE_REGEXS_STRINGS[i]}`;
}

// The minimum number of digits a phone number can contain.
// That's because the PHONE_REGEXS_STRINGS patterns are quite wide and report a lot of false positives.
const PHONE_MIN_DIGITS = 7;

// These are patterns that might be matched by PHONE_REGEXS_STRINGS,// but which are most likely not phone numbers. Add more patterns as needed.
const SKIP_PHONE_REGEXS = [
    // 2018-11-10
    '^[0-9]{4}-[0-9]{2}-[0-9]{2}$',];
const PHONE_REGEX_GLOBAL = new RegExp(`(${PHONE_REGEXS_STRINGS.join('|')})`,'ig');
const PHONE_REGEX = new RegExp(`^(${PHONE_REGEXS_STRINGS.join('|')})$`,'i');
const SKIP_PHONE_REGEX = new RegExp(`^(${SKIP_PHONE_REGEXS.join('|')})$`,'i');

/**
 * The function attempts to extract phone numbers from a text. Please note that
 * the results might not be accurate,since phone numbers appear in a large variety of formats and conventions.
 * If you encounter some problems,please [file an issue](https://github.com/apify/apify-js/issues).
 * @param {string} text Text to search the phone numbers in.
 * @return {string[]} Array of phone numbers found.
 * If no phone numbers are found,the function returns an empty array.
 * @memberOf social
 */
const phonesFromText = (text) => {
    if (!_.isstring(text)) return [];

    let phones = text.match(PHONE_REGEX_GLOBAL) || [];
    phones = phones.filter((phone) => {
        if (!phone) return false;

        // Skip too short phones,they are most likely incorrect
        if (phone.match(/[0-9]/g).length < PHONE_MIN_DIGITS) return false;

        // Skip phone numbers matching specific patterns
        if (SKIP_PHONE_REGEX.test(phone)) return false;

        return true;
    });

    return phones;
};

解决方法

如果您是 Python 的新手,那么习惯它是一种很棒的方式!
建议你自己翻译一下代码,
我将为您提供一些指导方针(按给定代码排序):

  1. 在python中const不是一回事,只需删除const这个词就可以了
  2. 你可能知道,但是 ';'也不是必需的
  3. python 中的循环有所不同,但对于您的示例来说可能更直观-
for i in len(PHONE_REGEXS_STRINGS):
    # code
  1. '{' 和 '}' 不是一个东西,但是需要制表符和 ':'('{' 所在的地方)。
  2. 关于正则表达式,我没有资格肯定地说,但我想它很相似
  3. 关于最后一段代码 - 我不太确定,但我想用另一种方式来实现相同的结果不会太难。
  4. python 中的函数如下所示:
def funcName(param1,param2): # types are not a thing here either
    # code
    # more code
    return param1 # return types are yet another not-a-thing
  1. 一些小点——“让”这个词可以删掉,“||”是“或”和“&&”是“和”和“!”是“不是”,“()”在 if、for 和类似的东西中不是必需的,单行代码完全可以,正如您所看到的 - 注释使用“#”而不是“//”。

祝你好运,你能做到!