2013-03-28 61 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)