2016-10-23 18 views
0

私はPythonを使用してPlistファイルからSpriteを抽出します。しかし、私はこのエラー "xml.etree.ElementTree.ParseError:整形式ではない(無効なトークン):行2、列101"を取得し、それを解決する方法を知らない。 誰でも私を助けることができます。どうもありがとうございます。 これは、Pythonのコードです:Pythonエラーxml.parsers.expat.ExpatError:整形式ではありません(無効なトークン):2行目、10列目1

import os 
import plistlib 
from PIL import Image 


class Matrix(object): 
    def __init__(self, src_box, clip_box, offset): 
     self.src_box = src_box 
     self.clip_box = clip_box 
     self.offset = offset 
     self.src_size = [self.src_box[2] - self.src_box[0], self.src_box[3] - self.src_box[1]] 
     self.clip_size = [self.clip_box[2] - self.clip_box[0], self.clip_box[3] - self.clip_box[1]] 


class TextureUnpacker(object): 
    @classmethod 
    def split_with_plist(cls, plist, save=None): 
     plist = os.path.abspath(plist) 
     if save is None: 
      save = plist + '_split' 
     else: 
      save = os.path.abspath(save) 

     dt = plistlib.readPlist(plist) 
     metadata, frames = dt['metadata'], dt['frames'] 
     format_version = metadata['format'] 
     big_img = Image.open(metadata['realTextureFileName']) 
     for frame, info in frames.iteritems(): 
      if format_version == 2: 
       info = cls.parse_as_plist_v2(info) 
      elif format_version == 3: 
       info = cls.parse_as_plist_v3(info) 
      else: 
       raise Exception('not support version' + str(format_version)) 
      cls.generate_little_image(big_img, info, os.path.join(save, frame)) 

    @classmethod 
    def generate_little_image(cls, big_img, info, path): 
     little_img = Image.new('RGBA', info['sz']) 
     region = big_img.crop(info['box']) 
     if info['rotated']: 
      # region = region.rotate(90, expand=1) 
      region = region.transpose(Image.ROTATE_90) 
     little_img.paste(region, info['xy']) 
     dir_ = os.path.dirname(path) 
     if not os.path.exists(dir_): 
      os.makedirs(dir_) 
     little_img.save(path) 

    @classmethod 
    def parse_as_plist_v2(cls, info): 
     """ 
     { 
      'frame': '{{1,1},{430,635}}', 
      'offset': '{2,-2}', 
      'rotated': False, 
      'sourceSize': '{639,639}' 
     } 
     """ 
     info['frame'] = cls.__convert_rect(info['frame']) 
     info['offset'] = cls.__convert_point(info['offset']) 
     info['sourceSize'] = cls.__convert_point(info['sourceSize']) 

     rotated = info['rotated'] 
     if rotated: 
      box = (info['frame'][0], info['frame'][1], 
        info['frame'][0] + info['frame'][3], 
        info['frame'][1] + info['frame'][2]) 
     else: 
      box = (info['frame'][0], info['frame'][1], 
        info['frame'][0] + info['frame'][2], 
        info['frame'][1] + info['frame'][3]) 

     x = info['offset'][0] + (info['sourceSize'][0] - info['frame'][2])/2 
     y = (info['sourceSize'][1] - info['frame'][3])/2 - info['offset'][1] 

     return { 
      'box': box, 
      'rotated': rotated, 
      'xy': (x, y), 
      'sz': info['sourceSize'] 
     } 

    @classmethod 
    def parse_as_plist_v3(cls, info): 
     """ 
     { 
      'aliases': [], 
      'spriteOffset': '{1,-1}', 
      'spriteSize': '{433,637}', 
      'spriteSourceSize': '{639,639}', 
      'textureRect': '{{1,1},{433,637}}', 
      'textureRotated': False 
     } 
     """ 
     info['spriteSize'] = cls.__convert_point(info['spriteSize']) 
     info['spriteOffset'] = cls.__convert_point(info['spriteOffset']) 
     info['textureRect'] = cls.__convert_rect(info['textureRect']) 
     info['spriteSourceSize'] = cls.__convert_point(info['spriteSourceSize']) 

     rotated = info['textureRotated'] 
     if rotated: 
      box = (info['textureRect'][0], info['textureRect'][1], 
        info['textureRect'][0] + info['textureRect'][3], 
        info['textureRect'][1] + info['textureRect'][2]) 
     else: 
      box = (info['textureRect'][0], info['textureRect'][1], 
        info['textureRect'][0] + info['textureRect'][2], 
        info['textureRect'][1] + info['textureRect'][3]) 

     x = info['spriteOffset'][0] + (info['spriteSourceSize'][0] - info['spriteSize'][0])/2 
     y = (info['spriteSourceSize'][1] - info['spriteSize'][1])/2 - info['spriteOffset'][1] 

     return { 
      'box': box, 
      'rotated': rotated, 
      'xy': (x, y), 
      'sz': info['spriteSourceSize'] 
     } 

    @classmethod 
    def __convert_rect(cls, rect): 
     s = rect.replace('{', '') 
     s = s.replace('}', '') 
     x, y, w, h = s.split(',') 
     return [int(x), int(y), int(w), int(h)] 

    @classmethod 
    def __convert_point(cls, pt): 
     s = pt.replace('{', '') 
     s = s.replace('}', '') 
     x, y = s.split(',') 
     return [int(x), int(y)] 


if __name__ == '__main__': 
    import sys 
    TextureUnpacker.split_with_plist(sys.argv[1]) 

そして、これはのplistファイルです:私はxmllintを通して、あなたのファイルを実行し、それを解析しません

<plist version="1.0"> 
    <dict> 
     <key>frames</key> 
     <dict> 
      <key>Sprite_ID1212</key> 
      <dict> 
       <key>frame</key> 
       <string>{{0, 0}, {640, 1156}}</string> 
       <key>offset</key> 
       <string>{0, -0}</string> 
       <key>rotated</key> 
       <string>false</string> 
       <key>sourceColorRect</key> 
       <string>{{0, 0}, {640, 1156}}</string> 
       <key>sourceSize</key> 
       <string>{640, 1156}</string> 
      </dict> 
      <key>Sprite_ID160</key> 
      <dict> 
       <key>frame</key> 
       <string>{{644, 0}, {640, 1156}}</string> 
       <key>offset</key> 
       <string>{0, -0}</string> 
       <key>rotated</key> 
       <string>false</string> 
       <key>sourceColorRect</key> 
       <string>{{0, 0}, {640, 1156}}</string> 
       <key>sourceSize</key> 
       <string>{640, 1156}</string> 
      </dict> 
      <key>Sprite_ID1113</key> 
      <dict> 
       <key>frame</key> 
       <string>{{1288, 0}, {640, 1156}}</string> 
       <key>offset</key> 
       <string>{0, -0}</string> 
       <key>rotated</key> 
       <string>false</string> 
       <key>sourceColorRect</key> 
       <string>{{0, 0}, {640, 1156}}</string> 
       <key>sourceSize</key> 
       <string>{640, 1156}</string> 
      </dict> 
     <key>metadata</key> 
     <dict> 
      <key>format</key> 
      <string>2</string> 
      <key>realTextureFileName</key> 
      <string>atlas_ID101.png</string> 
      <key>size</key> 
      <string>{2048, 2048}</string> 
      <key>smartupdate</key> 
      <string>$Potion:Smartupdate:$</string> 
      <key>textureFileName</key> 
      <string>atlas_ID101.png</string> 
     </dict> 
    </dict> 
</plist> 
+0

文書の最後に閉じるタグがありません。しかし、唯一の問題ではないかもしれません。 –

+0

ああ、plistファイルが長すぎるので、その一部を書きます。 – thefriend

+0

エラーメッセージはファイルに問題があると思われるので、どこにあるのかを確認してください。最小限のplistファイルから始めて、より多くのコンテンツを追加するか、完全なファイルから始めて、解析するまで部分を削除することができます。最小限の作業例に関する一般的なヒントも適用されます。https://stackoverflow.com/help/mcve –

答えて

0

。 4行目の<dict>タグには対応する</dict>タグがありません。タグを削除するか、タグを閉じる適切な場所を見つけてください。私は入れ子になった<dict>構造が問題を混乱させていると思う。

関連する問題