0

I have to take the values from a text file which contains the co-ordinates to draw characters out in TurtleWorld, an example of the text file is the following:

<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>

I have to then write a function to take all of these points and then convert them into a dictionary where a key is the character and the corresponding values are the set of points which can be used to draw that character in TurtleWorld.

The code I have tried is the following:

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] != '/':                             
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
George Burrows
  • 3,391
  • 9
  • 31
  • 31

1 Answers1

0

take a look at http://oreilly.com/catalog/pythonxml/chapter/ch01.html :: specifically, hit up the example titled :: Example 1-1: bookhandler.py

you can more or less credit/copy that and tweak it to read your particular xml. once you get the 'guts'(your coords), you can split it into a list of x/y coords really easily

such as

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

and chunk it into a list w/ groups of 2 How do you split a list into evenly sized chunks? and store the result letters[letter] = result

or you can do the chunking more funky using the re module

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]]
Community
  • 1
  • 1
pyInTheSky
  • 1,459
  • 1
  • 9
  • 24