2013-03-28 62 views
1

我需要编写一个程序,给出了如下表的输出:打印字符映射表

chr:  ! " # $ % & ' ( ) * + , - . / 
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 
chr: @ A B C D E F G H I J K L M N O 
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
chr: P Q R S T U V W X Y Z [ \ ] ^ _ 
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 
chr: ` a b c d e f g h i j k l m n o 
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
chr: p q r s t u v w x y z { | } ~  
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 

任何帮助,将不胜感激,但我想问一下不给出完整的答案,而是提示,所以对我来说有一些挑战。谢谢。

+0

告诉我们更多关于您的约束和要求。就目前来看,一个调用'print'的格式化表格已经足够了。 –

+0

@Robᵩ我唯一的约束是我必须通过for循环来做到这一点。 – user2080719

回答

6

ordchr功能将帮助你:

ord('a') # 97 
chr(97) # 'a' 

添加到range,和你有一个炖怎么回事!

+1

那么,你需要两个'范围'(在两个嵌套'for'循环中......),否则,这看起来可能与OP想要的一样多。 – abarnert

+0

够正确! :-) –

+0

是的,这就是我需要的。谢谢! – user2080719

1
print '''chr: ! " # $ % & ' () * + , - ./
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
chr: 0 1 2 3 4 5 6 7 8 9 : ; <=> ? 
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 
chr: @ A B C D E F G H I J K L M N O 
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
chr: P Q R S T U V W X Y Z [ \ ]^_ 
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 
chr: ` a b c d e f g h i j k l m n o 
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
chr: p q r s t u v w x y z { | } ~ 
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127''' 
+0

我认为,但我正在考虑更多沿循for循环。但我猜这可能会奏效。 – user2080719

+0

在这种情况下,看哈尔金丝雀的回答 –

+2

聪明,但我认为它不会是更好地使用'requests'和'bs4'读取来自http://stackoverflow.com/questions/15692127/printing-问题字符映射而不是复制粘贴它? :) – abarnert

3
for i in range(32,128): 
    print (i, chr(i)) 

,或者是更接近你想要什么:

#!/usr/bin/python3 
def f(x,y): 
    for i in range(x,y): 
     print ('%3d '%i,end=''), 
    print() 
    for i in range(x,y): 
     print ('%3s '%chr(i),end='') 
    print() 
for x in range(32,128,16): 
    f(x,x+16) 
1

提示:与这样给定范围内区分线的情况下,循环利用的循环。转换后的字符可以作为字符串添加在一起,并且只有在添加了所有字符串后才打印该行。

完整的代码是:

for i in range (2,14): 
#range(1,13 would have been correct as well, but I want to use the parity) 
if i%2==0: #use even numbers for "chr..." line 
    a="chr: " 
    for j in range(int((i/2-1)*16+32),int((i/2-1)*16+48)): 
#range is a bit complicated due to the i-range 
    b=str(chr(j)) 
#the following ifs are used to regulate space depending on character length 
    if len(b)==1: 
     s=" " 
    if len(b)==2: 
     s=" " 
    if len(b)==3: 
     s=" " 
    a=a+b+s #add new characters with space to the previous ones 
    print(a) 
if i%2==1: #use odd numbers for asc:... line 
    a="asc: " 
    for j in range(int(((i-1)/2-1)*16+32),int(((i-1)/2-1)*16+48)): 
    b=str(j) #in this line you need only the numbers 
#the following ifs are used to regulate space depending on character length 
    if len(b)==1: 
     s=" " 
    if len(b)==2: 
     s=" " 
    if len(b)==3: 
     s=" " 
    a=a+b+s 
    print(a)