2017-04-02 8 views
2

小さなインテリアデザインアプリを作ろうとすると、私はtxtファイルを入力して、自分のプログラムがシェル上のグリッドを返すようにします。 高さと幅が両方とも20のグリッドを作成する方法を知りたいだけです。誰もtxtファイルに基づいてシェルにグリッドを返す方法を知っていますか?

これは私がこれまで持っていたコードです。高さではなく幅を作る方法しか知りません。私はまた、私のtxtファイルから数字と文字を取得する方法がわからないが、私はラインごとにリストに自分のtxtファイルを作った。

f = open('Apt_3_4554_Hastings_Coq.txt','r') 
bigListA = [ line.strip().split(',') for line in f ] 

offset = " " 
width = 20 
string1 = offset 
for number in range(width): 
    if len(str(number)) == 1: 
     string1 += " " + str(number) + " " 
    else: 
     string1 += str(number) + " " 
print (string1) 
+0

あなたは現在何をしていますかe?あなたは内と外の例を挙げることができますか? – abccd

+0

私のコードは幅を作るだけですが、高さの機能を作る方法やtxtファイルから情報を得る方法はわかりません。 –

答えて

1

やり過ぎのビットが、その周りclassを作るために楽しかった:

def decimal_string(number, before=True): 
    """ 
    Convert a number between 0 and 99 to a space padded string. 

    Parameters 
    ---------- 
    number: int 
     The number to convert to string. 
    before: bool 
     Whether to place the spaces before or after the nmuber. 

    Examples 
    -------- 
    >>> decimal_string(1) 
    ' 1' 
    >>> decimal_string(1, False) 
    '1 ' 
    >>> decimal_string(10) 
    '10' 
    >>> decimal_string(10, False) 
    '10' 
    """ 
    number = int(number)%100 
    if number < 10: 
     if before: 
      return ' ' + str(number) 
     else: 
      return str(number) + ' ' 
    else: 
     return str(number) 

class Grid(object): 
    def __init__(self, doc=None, shape=(10,10)): 
     """ 
     Create new grid object from a given file or with a given shape. 

     Parameters 
     ---------- 
     doc: file, None 
      The name of the file from where to read the data. 
     shape: (int, int), (10, 10) 
      The shape to use if no `doc` is provided. 
     """ 
     if doc is not None: 
      self.readfile(doc) 
     else: 
      self.empty_grid(shape) 

    def __repr__(self): 
     """ 
     Representation method. 
     """ 
     # first lines 
     # 0 1 2 3 4 5 6 7 ... 
     # - - - - - - - - ... 
     number_line = ' ' 
     traces_line = ' ' 
     for i in range(len(self.grid)): 
      number_line += decimal_string(i) + ' ' 
      traces_line += ' - ' 
     lines = '' 
     for j in range(len(self.grid[0])): 
      line = decimal_string(j, False) + '|' 
      for i in range(len(self.grid)): 
       line += ' ' + self.grid[i][j] + ' ' 
      lines += line + '|\n' 
     return '\n'.join((number_line, traces_line, lines[:-1], traces_line)) 

    def readfile(self, doc): 
     """ 
     Read instructions from a file, overwriting current grid. 
     """ 
     with open(doc, 'r') as open_doc: 
      lines = open_doc.readlines() 
     shape = lines[0].split(' ')[-2:] 
     # grabs the first line (line[0]), 
     # splits the line into pieces by the ' ' symbol 
     # grabs the last two of them ([-2:]) 
     shape = (int(shape[0]), int(shape[1])) 
     # and turns them into numbers (np.array(..., dtype=int)) 
     self.empty_grid(shape=shape) 
     for instruction in lines[1:]: 
      self.add_pieces(*self._parse(instruction)) 

    def empty_grid(self, shape=None): 
     """ 
     Empty grid, changing the shape to the new one, if provided. 
     """ 
     if shape is None: 
      # retain current shape 
      shape = (len(self.grid), len(self.grid[0])) 
     self.grid = [[' ' for i in range(shape[0])] 
          for j in range(shape[1])] 

    def _parse(self, instruction): 
     """ 
     Parse string instructions in the shape: 
      "C 5 6 13 13" 
     where the first element is the charachter, 
     the second and third elements are the vertical indexes 
     and the fourth and fifth are the horizontal indexes 
     """ 
     pieces = instruction.split(' ') 
     char = pieces[0] 
     y_start = int(pieces[1]) 
     y_stop = int(pieces[2]) 
     x_start = int(pieces[3]) 
     x_stop = int(pieces[4]) 
     return char, y_start, y_stop, x_start, x_stop 

    def add_pieces(self, char, y_start, y_stop, x_start, x_stop): 
     """ 
     Add a piece to the current grid. 

     Parameters 
     ---------- 
     char: str 
      The char to place in the grid. 
     y_start: int 
      Vertical start index. 
     y_stop: int 
      Vertical stop index. 
     x_start: int 
      Horizontal start index. 
     x_stop: int 
      Horizontal stop index. 

     Examples 
     -------- 
     >>> b = Grid(shape=(4, 4)) 
     >>> b 
      0 1 2 3 
      - - - - 
     0 |   | 
     1 |   | 
     2 |   | 
     3 |   | 
      - - - - 
     >>> b.add_pieces('a', 0, 1, 0, 0) 
     >>> b 
      0 1 2 3 
      - - - - 
     0 | a   | 
     1 | a   | 
     2 |   | 
     3 |   | 
      - - - - 
     >>> b.add_pieces('b', 3, 3, 2, 3) 
     >>> b 
      0 1 2 3 
      - - - - 
     0 | a   | 
     1 | a   | 
     2 |   | 
     3 |  b b | 
      - - - - 
     """ 
     assert y_start <= y_stop < len(self.grid[0]),\ 
       "Vertical index out of bounds." 
     assert x_start <= x_stop < len(self.grid),\ 
       "Horizontal index out of bounds." 
     for i in range(x_start, x_stop+1): 
      for j in range(y_start, y_stop+1): 
       self.grid[i][j] = char 

あなたは、その後でfile.txt持つことができます。

20 20 
C 5 6 13 13 
C 8 9 13 13 
C 5 6 18 18 
C 8 9 18 18 
C 3 3 15 16 
C 11 11 15 16 
E 2 3 3 6 
S 17 18 2 7 
t 14 15 3 6 
T 4 10 14 17 

をしてます

>>> a = Grid('file.txt') 
>>> a 
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
    - - - - - - - - - - - - - - - - - - - - 
0 |               | 
1 |               | 
2 |   E E E E          | 
3 |   E E E E       C C   | 
4 |           T T T T  | 
5 |          C T T T T C | 
6 |          C T T T T C | 
7 |           T T T T  | 
8 |          C T T T T C | 
9 |          C T T T T C | 
10|           T T T T  | 
11|            C C   | 
12|               | 
13|               | 
14|   t t t t          | 
15|   t t t t          | 
16|               | 
17|  S S S S S S          | 
18|  S S S S S S          | 
19|               | 
    - - - - - - - - - - - - - - - - - - - - 
+0

すごくありがとう! –

+0

もう一度、[それは過剰です - それを賢明に使う](https://en.wikipedia.org/wiki/Economy_of_force)。どのビットとピースが有用であるか試してみて、どのようにして自分のケースに合わせてカスタマイズできるのか理解する。 – berna1111

1

部屋を20x20グリッドで表すことができます。 1つのアイデアは、listlistです。個人的に私はdictが好きです。

ファイルを読み、各ポイントを割り当てます。 (それはあなたが投稿問題ではなかったので、私は、あなたがファイルを解析扱ってきたと仮定します。)例えば:

room[5, 13] = 'C' 

次に、あなたがあなたの出力を提供するために、座標を反復処理することができます。

for i in range(N_ROWS): 
    for j in range(N_COLS): 
     # Print the character if it exists, or a blank space. 
     print(room.get((i, j), default=' '), end='') 
    print() # Start a new line. 
関連する問題