简体   繁体   English

我怎样才能得到与个位数一致的两位数的数字坐标列的最后一位

[英]How can i get the number coordinate column last digit of the double digits in line with the single digits

there's a grid in a seperate text file and the function reads this file and returns an object consisting of the grid where the text file is read line by line单独的文本文件中有一个网格,function 读取该文件并返回一个 object,其中包含逐行读取文本文件的网格

my issue is that in my numerical coordinates the single digit doesn't align with the last digit of the double digit numbers我的问题是在我的数字坐标中,单个数字与双位数的最后一位数字不对齐

def load_board(filename):
    result = "  "
    with open(filename) as f:
        print(f)
        for index, line in enumerate(f):
            if index == 0:
                result += ' '+' '.join([chr(alphabets + 65) for alphabets in range(len(line) - 1)]) + '\n' #the alphabetical column heading

            result += f"{-(index + 1)+(20)}"
            if (len(result))<2:
                 result+=' '.join(result)
       
            for characters in line:
                result += " " + (characters)
        return result

def save_board(filename, board):
    with open(filename, "wt") as f:
        f.write(board)

b = load_board("l19.txt")
print(b)
save_board("l19b.txt", b)

this gives me an output of这给了我 output 的

   A B C D E F G H I J K L M N O P Q R S
19 . . . . @ @ @ . O O . . @ . O O O . O 
18 @ O O @ O @ . . @ O @ O . . . @ @ . @ 
17 @ O . . @ O . O O O O O . O O O O O @ 
16 . . @ @ . O O . @ . . O @ O . @ . O . 
15 O . @ . @ . O @ . O O @ @ O . . O @ O 
14 O . . . O O O @ . @ @ . . . @ . O @ @ 
13 . . @ O @ . . @ . . O O O . @ . @ . . 
12 . . @ @ . @ @ @ . . . @ O . O . . . @ 
11 @ O . . @ . @ @ @ @ O . . @ O O O @ O 
10 @ . . O . @ @ O @ O O @ @ . @ . O @ . 
9 @ O O O . . @ O . . @ @ O @ @ @ . O O 
8 @ @ O @ . O O O . @ . O @ . @ @ @ . @ 
7 @ . O . O @ O O . O O . @ O @ . . @ O 
6 @ . . . O @ @ O O @ . @ @ . . O . O . 
5 O O @ @ . . O @ @ . @ . @ . O @ @ O . 
4 @ . O . . O O . @ O @ O @ O O . @ @ . 
3 @ @ O O @ . O . @ . O @ . @ O @ O . . 
2 . . . O O @ @ O . @ O . O . @ O O @ . 
1 @ @ . @ O . @ @ . . @ O O O O O @ @ @

i tried to get it aligned by adding a conditional statement stating that if the length of the number is less than 2 to add a space however it didn't work我试图通过添加一个条件语句来使其对齐,该条件语句声明如果数字的长度小于 2 以添加空格但是它不起作用

Use the options of f-string to format as a 2 length value, so you can remove the useless if (len(result))<2: block使用 f-string 的选项格式化为 2 长度值,因此您可以删除无用的if (len(result))<2:

You can also simplify using string.ascii_uppercase您还可以使用string.ascii_uppercase进行简化

from string import ascii_uppercase

def load_board(filename):
    result = "  "
    with open(filename) as f:
        for index, line in enumerate(f):
            # the alphabetical column heading
            if index == 0:
                result += ' ' + ' '.join(ascii_uppercase[:len(line) - 1]) + '\n'
            # the number prefix
            result += f"{19 - index:2d}"
            # the chars
            result += ' ' + ' '.join(line.strip()) + '\n'
    return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM