【问题标题】:Python - Comparing strings from a file [closed]Python - 比较文件中的字符串[关闭]
【发布时间】:2013-02-12 19:36:53
【问题描述】:

我正在学习 Python 以在 Inkscape 中进行扩展,但在比较从文件中加载的字符串时遇到了问题。我要做的是加载我在文本文件中定义的多边形:

    polygon
    r:255
    g:0
    b:0
    50;50
    50;100
    100;50

我的解析方法是这样的:

    def load_file(filepath, parent, log):
        file = open(filepath)
        x = []
        y = []
        r = 0
        g = 0
        b = 0
        index = 0

        for line in file:
            fline = line.lstrip("\xef\xbb\xbf").rstrip("\n")
            log.write("Input string: " + repr(line) + "\n")
            log.write("Formatted: " + repr(fline) + "\n")
            if fline == "":
                continue
            elif fline is "polygon": ## Where the first line should be going
                log.write("\tDetected string as polygon start delimiter\n")
                if index > 0:
                    draw_shape(x, y, r, g, b, "Polygon", parent)
                    del x[0, len(x)]
                    del y[0, len(y)]
                    r = g = b = index = 0
                continue
            elif fline[:2] is "r:":
                log.write("\tDetected string as polygon red value delimiter\n")
                r = int(fline[2:])
                continue
            elif fline[:2] is "g:":
                log.write("\tDetected string as polygon green value delimiter\n")
                g = int(fline[2:])
                continue
            elif fline[:2] is "b:":
                log.write("\tDetected string as polygon blue value delimiter\n")
                b = int(fline[2:])
                continue
            else: ## Where the first line actually is going
                log.write("\tDelimiter failed previous detections; assumed to be polygon cordinates\n")
                spl = fline.split(";")
                x[index] = float(spl[0]) ## Error gets thrown here
                y[index] = float(spl[1])
                index += 1
                continue

        draw_shape(x, y, r, g, b, parent)

这个方法在第一行就出错了。它不断看到“多边形”并进入最后一个 else 块,在那里它解析坐标。我一直保留的日志文件如下所示:

    Process Started
    Input string: '\xef\xbb\xbfpolygon\n'
    Formatted: 'polygon'
        Delimiter failed previous detections; assumed to be polygon coordinates

我已经在 shell 中逐步完成了这个过程,它说line is "process" 是真的,所以我完全迷失了这里。有什么帮助吗?

【问题讨论】:

  • 不要使用is 与除None 之外的任何内容进行比较。 (使用== 比较是否相等)。
  • @Wooble 我想我要朝某人的脸开枪了……我知道那是愚蠢的事情。介意把它放在答案中以便我标记它吗?

标签: python string file


【解决方案1】:
  1. 比较fline is "polygon" 几乎总是错误的。请改用fline == "polygon"

  2. 这与您的问题无关,但如果您使用正确的 Unicode 解码功能,而不是手动剥离字节顺序标记并将其余部分视为字节,您将更轻松地处理文本。我更喜欢codecs.open(filename, encoding='utf-8-sig')

【讨论】:

    【解决方案2】:

    一旦你成功打开了 Unicode 文件,我认为这样的事情比你现在做的要容易:

    elements='''polygon
    r:255
    g:0
    b:0
    50;50
    50;100
    100;50
    
    polygon
    r:155
    g:22
    b:55
    55;60
    66;100
    120;150
    155;167'''       
    
    for element in re.split(r'^\s*\n',elements,flags=re.MULTILINE):
        if element.startswith('polygon'):
            el=element.splitlines()
            poly={k:v for k,v in [s.split(':') for s in el[1:4]]}
            x,y=zip(*[s.split(';') for s in el[4:]])
            poly.update({'x':x, 'y': y})
            print poly
    

    打印:

    {'y': ('50', '100', '50'), 'x': ('50', '50', '100'), 'r': '255', 'b': '0', 'g': '0'}
    {'y': ('60', '100', '150', '167'), 'x': ('55', '66', '120', '155'), 'r': '155', 'b': '55', 'g': '22'}
    

    【讨论】:

    • 不太确定我在看什么。
    • 你现在可以这样做了:draw_shape(poly['x'], poly['y'], poly['r'], poly['g'], poly['b'], ...)
    • 嗯,我知道这个函数基本上是一个多边形对象,它是一个字典,在键中保存与文本文件中的值相对应的元组,但至于它是如何做到的,我只可以猜到它看起来和LINQ很像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多