RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1378394
Accepted
ардуино IDE
ардуино IDE
Asked:2022-07-04 16:34:34 +0000 UTC2022-07-04 16:34:34 +0000 UTC 2022-07-04 16:34:34 +0000 UTC

在 Python 中不使用库进行解析 [关闭]

  • 772
关闭 这个问题是题外话。目前不接受回复。

仅当您在提出问题之前尝试自己解决问题时,才允许将学习问题作为问题。请编辑问题并指出究竟是什么导致您难以解决问题。例如,请提供您在尝试解决问题时编写的代码

8 个月前关闭。

改进问题

我有这一行,它需要在不使用库的情况下进行解析,只使用 python 3 本身:

command s1 s2 (l1 l2) [lp 990] (l [li p]) "entity j [90 90]" <ping 90> *9 9* lol

需要获得:

['command', 's1', 's2', {'strings': ['l1', 'l2'], type: 'round_brackets'}, {'strings': ['lp', '990'], type: 'square_brackets'}, {'strings': ['l', {'strings': ['li', 'p'], type: 'square_brackets'}], type: 'round_brackets'}, {'strings': 'entity j [90 90]', type: 'quotes'}, {'strings': ['ping', '90'], type: 'command'}, {'integers': ['9', '9'], type: 'nums'}, 'lol']

解析规则:

  1. 括号和引号和星号可以嵌套
  2. 解析在引号中不起作用。
  3. 用星星代替字符串整数。
  4. 在结果数组中,其字典将包含内容本身和类型。

这就是您需要做的所有事情,请谁能做到:)。

而且我无法使用列表中的字典编写解析器。

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Danis
    2022-07-04T18:08:25Z2022-07-04T18:08:25Z
    def f(s):
        start = 0
        bracket = None
        array = []
        for i in range(len(s)):
            if bracket:
                if s[i] == ")]>*\""[bracket[1]]:
                    array.append(s[start:i + 1].strip())
                    start = i + 1
                    bracket = None
                continue
            if s[i] in "([<*\"":
                if s[start:i].strip():
                    array.extend(s[start:i].split())
                start = i
                bracket = s[i], "([<*\"".index(s[i])
        
        if s[start:].strip():
            array.extend(s[start:].strip().split())
        
        result = []
        for el in array:
            if el[0] not in "([<*\"":
                result.append(el)
            elif el[0] == "\"":
                result.append({
                    "strings": el[1:-1],
                    "type": "quotes",
                })
            elif el[0] == "*":
                result.append({
                    "integers": list(map(int, f(el[1:-1]))),
                    "type": "nums",
                })
            else:
                type_ = {
                    "(": "round_brackets",
                    "[": "square_brackets",
                    "<": "command",
                }[el[0]]
                
                result.append({
                    "strings": f(el[1:-1]),
                    "type": type_,
                })
    
        return result
    
    s = '''command s1 s2 (l1 l2) [lp 990] (l [li p]) "entity j [90 90]" <ping 90> *9 9* lol'''
    print(f(s))
    
    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5