2011-12-15 11 views
0
私はTurtleWorld中から文字を描画する座標を含むテキストファイルから値を取らなければならない

を使用して、テキストファイルの例は以下の通りである。読書やフォントファイルを編集し、辞書

<character=B, width=21, code=66> 
4 21 
4 0 
-1 -1 
4 21 
13 21 
16 20 
17 19 
18 17 
18 15 
17 13 
16 12 
13 11 
-1 -1 
4 11 
13 11 
16 10 
17 9 
18 7 
18 4 
17 2 
16 1 
13 0 
4 0 
</character> 

これらの点をすべて取って、キーが文字で、対応する値がTurtleWorldでその文字を描画するために使用できるポイントのセットである辞書に変換する関数を記述する必要があります。

私がしようとしたコードは次のとおりです。

def read_font(): 
    """ 
    Read the text from font.txt and convert the lines into instructions for how to plot specific characters 
    """ 

    filename = raw_input("\n\nInsert a file path to read the text of that file (or press any letter to use the default font.txt): ") 
    if len(filename) == 1: 
     filename = 'E:\words.txt' 
     words = open(filename, 'r') 
    else: 
     words = open(filename, 'r') 

    while True:                     # Restarts the function if the file path is invalid 
     line = words.readline() 
     line = line.strip() 
     if line[0] == '#' or line[0] == ' ':             # Used to omit the any unwanted lines of text 
      continue 
     elif line[0] == '<' and line[1] == '/':             # Conditional used for the end of each character 
      font_dictionary[character] = numbers_list 
     elif line[0] == '<' and line[1] != '/':        
+1

を使用してチャンクよりファンキーな操作を行うことができますが、コードの非常に多くを持っています ファイル... –

答えて

0

http://oreilly.com/catalog/pythonxml/chapter/ch01.htmlを見てみましょう::具体的には、::例1-1と題した例打つ:bookhandler.py

することができますが多かれ少なかれ、あなたの特定のXMLを読むためにそれを微調整してください。あなたは「ガッツ」(あなたのCOORDS)を得れば、あなたは本当に簡単にX/Y COORDSのリストにそれを分割することができます

など2のグループ/ wのリストに

a = "1 3\n23 4\n3 9\n" 
coords = map(int,a.split()) 

とチャンクそれとしてHow do you split a list into evenly sized chunks? 、それは読書のためではありません結果の手紙[文字]を保存する=

を引き起こすか、再モジュール

import re 
a = "1 13\n4 5\n" 
b = re.findall("\d+ *\d+",a) 
c = [map(int,item.split()) for item in b] 
c 
[[1, 13], [4, 5]]