将TCL列表转换为Python列表



我尝试将TCL列表转换为Python列表。

有两个问题:

  • 如果原始列表中的列表仅包含一个列表,则翻译不正确。例如,{{12 34}}未正确翻译。
  • 将所有数字转换为类型的选项不起作用。

Python 3代码:

import tkinter

class TclInterpreter(object):
    def __init__(self):
        self._tcl = tkinter.Tcl()
    def eval(self, tcl_cmd):
        return self._tcl.eval(tcl_cmd)

class TclPyListTranslator(object):
    def __init__(self, tcl):
        self._tcl = tcl
    def to_py(self, tcl_list, dtype=str):
        # convert a Tcl List to python list, also convert elements of each leaf
        # node to dtype
        self._tcl.eval("set tcl_list %s" % tcl_list)
        numItems = int(self._tcl.eval("llength $tcl_list"))
        if numItems > 1:
            result = [self._tcl.eval("lindex $tcl_list %d" % i) for i in range(
                numItems)]
            for i in range(numItems):
                result[i] = self.to_py("{" + result[i] + "}", dtype)
        else:
            result = dtype(self._tcl.eval("lindex $tcl_list %d" % 0))
        return result

inter = TclInterpreter()
translator = TclPyListTranslator(inter)
tcl_list = "{12 {{12 34}} {56 {78 {11 12} 10}}}"
# prints ['12', '12 34', ['56', ['78', ['11', '12'], '10']]]
# The '12 34' is incorrect
print(translator.to_py(tcl_list))
# does not run
print(translator.to_py(tcl_list, int))

python parser:

def add_element(cache, element):
    if element != '':
        cache[-1].append(element)
    return ''
def parse(tcl_list):
    """ Parse TCL list to Python list """    
    out = []
    cache = [out]
    element = ''
    escape = False
    for char in tcl_list:
        if escape:
            element += char
            escape = False
        elif char == "\":
            escape = True
        elif char in [" ", "t", "r", "n"]:
            element = add_element(cache, element)
        elif char == "{":
            a = []
            cache[-1].append(a)
            cache.append(a)
        elif char == "}":
            element = add_element(cache, element)
            cache.pop()
        else:
            element += char
    return out[0]
import pprint
pprint.pprint(
    parse("{ 12 apple {100} {} {{12 34}} n {56n { \{78 {11 12 11} 10}}}"))

输出:

['12',
 'apple',
 ['100'],
 [],
 [['12', '34']],
 ['56', ['{78', ['11', '12', '11'], '10']]]

处理此操作的最简单方法是将代码放在TCL侧(本地理解TCL列表(以生成Python值的字符串形式,然后在Python中生成eval。但是,复杂的部分是TCL的类型系统与Python的类型系统确实完全不同(以至于我不打算解释它,因为它是一个非常复杂且技术上的论点(,从而决定了嵌套列表的叶子的位置结构非平凡。需要一些假设。通过这些假设,我们可以在不多的代码中做一个相当不错的工作。

您需要的TCL侧代码是这样的(在需要整数的叶子的情况下(:

proc toPythonList {value} {
    if {[string is integer -strict $value]} {
        return $value
    }
    set result "["
    foreach item $value {
        append result [toPythonList $item] ", "
    }
    append result "]"
    return $result
}

那意味着您可以执行此操作(并且我已经为不同类型的叶子的改编添加了一个非常简单的版本(:

class TclPyListTranslator(object):
    def __init__(self, tcl):
        self._tcl = tcl
        self._tcl.eval("""
            proc isLeaf.int {value} {
                string is integer -strict $value
            }
            proc isLeaf.str {value} {
                expr {![string match "{*}" $value]}
            }
            proc toPythonLeaf.int {value} { return $value }
            proc toPythonLeaf.str {value} { return ""$value"" }
            proc toPythonList {value dtype} {
                if {[isLeaf.$dtype $value]} {
                    return [toPythonLeaf.$dtype $value]
                }
                set result "["
                foreach item $value {
                    append result [toPythonList $item] ", "
                }
                append result "]"
                return $result
            }
        """)
    def to_py(self, tcl_list, dtype=str):
        # convert a Tcl List to python list
        return eval(self._tcl.eval("toPythonList %s %s" % (tcl_list, dtype.__name__))

警告:上面的代码应该工作,但是我无法测试它,因为我没有在任何Python解释器中配置TKINTER。这些作品自行起作用,所以我很自信。

我今天需要这样做,并将接受的答案作为起点,但是,它没有考虑到包含空格的字符串。例如:{hello world "foo bar"}将导致['hello', 'world', '"foo', 'bar"']而不是['hello', 'world', 'foo bar']

这是一个修改后的实现:

class TCLListParser(object):
    NO_ESCAPE = 0
    SINGLE_ESCAPE = 1
    STRING_ESCAPE = 2
    def __init__(self):
        self._out = None
        self._buffer = None
        self._stack = None
    def _flush(self):
        if self._buffer is not None:
            self._stack[-1].append(self._buffer)
        self._buffer = None
    def _add_char(self, char):
        if self._buffer is None:
            self._buffer = char
        else:
            self._buffer += char
    def parse(self, tcl_list):
        self._out = []
        self._stack = [self._out]
        self._buffer = None
        escape = self.NO_ESCAPE
        for char in tcl_list:
            # Single escapes
            if escape & self.SINGLE_ESCAPE:
                self._add_char(char)
                escape &= ~self.SINGLE_ESCAPE
            elif char == '\':
                escape |= self.SINGLE_ESCAPE
            # Strings with spaces, like "hello world"
            elif char == '"':
                escape ^= self.STRING_ESCAPE
            else:
                if escape & self.STRING_ESCAPE:
                    self._add_char(char)
                elif char in [" ", "t", "r", "n"]:
                    self._flush()
                elif char == "{":
                    _ = []
                    self._stack[-1].append(_)
                    self._stack.append(_)
                elif char == "}":
                    self._flush()
                    self._stack.pop()
                else:
                    self._add_char(char)
        return self._out[0]
parser = TCLListParser()
pprint.pprint(parser.parse('{ 12 "big apple" {100} {} {{12 34}} n {56n { \{78 {11 12 11} 10}}}'))

结果:

['12',
 'big apple',
 ['100'],
 [],
 [['12', '34']],
 ['56', ['{78', ['11', '12', '11'], '10']]]

最新更新