1

Inkscape で拡張機能を作成するために Python を学習しているところですが、ファイルから読み込んだ文字列を比較する際に問題が発生しています。私がやろうとしているのは、テキスト ファイルで定義したポリゴンを読み込むことです。

    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

私はシェルでプロセスを実行しましたが、そこにline is "process"は本当だと書かれているので、ここで完全に迷っています。何か助けはありますか?

4

2 に答える 2

1

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'}
于 2013-02-12T21:32:47.523 に答える
1
  1. 比較fline is "polygon"はほとんど常に偽になります。fline == "polygon"代わりに使用してください。

  2. 問題とは関係ありませんが、バイトオーダーマークを手動で削除して残りをバイトとして扱うのではなく、適切な Unicode デコード関数を使用すると、テキストの処理が簡単になります。私は好きcodecs.open(filename, encoding='utf-8-sig')です。

于 2013-02-12T20:06:56.243 に答える