2011年10月18日 星期二

[Linux]...... 應該是超完整版的Unix 發展大系

http://www.levenez.com/unix/unix_a4.pdf

@@~~好在考試不考這個.........

『1977年之後 UNIX Fork 出那些支系........』 ~~~~~~ 神經病才記得住!!!!!!!!

[Learning Python] Sequence Data Type - String - 字串輸出格式控制

    Python 提供兩種字串輸出格式控制的方式,第一種與『C Language』"printf" 的輸出格式控制方法相類似;第二種則需要 call 字串輸出格式的 method。

    a. 像『C Language』"printf" 的 String formatting expressions

        這個方式需要用到下列資料型別的代碼

Code                                  Meaning
-------------------------------------------
s                            String (or any object’s str(X) string)
r                            s, but uses repr, not str
c                           Character
d                          Decimal (integer)
i                            Integer
u                          Same as d (obsolete: no longer unsigned)                                  
o                         Octal integer
x                          Hex integer
X                          x, but prints uppercase
e                          Floating-point exponent, lowercase
E                          Same as e, but prints uppercase
f                          Floating-point decimal
F                         Floating-point decimal
g                         Floating-point e or f
G                         Floating-point E or F
%                        Literal %




















    語法如下,

'%[flags][width][.precision]typecode'    %(Value)                   



    整數的輸出格式控制大概會遇到下列幾種狀況,


#整數
>>> '%d' %(77)
'77'

#%+<typecode >   :  加正負號
>>> '%+d  %+d' %(100,-100)
'+100  -100'

#%<Width><typecode >   :   控制輸出位數
>>> '%4d %6d %2d' %(100,100,100)
' 100    100 100'

#%<0Width><typecode >   :   控制輸出位數,不足位補'0'                                                 
>>> '%04d %06d %02d' %(100,100,100)
'0100 000100 100'

#%<-Width><typecode >   :   控制輸出位數,向左靠攏
>>> '%4d %6d %2d' %(100,100,100)
' 100    100 100'
>>> '%-4d %-6d %-2d' %(100,100,100)
'100  100    100'








 

    浮點數的輸出格式控制大概會遇到下列幾種狀況,

#浮點數
>>> '%f' %(100.123)
'100.123000'

#%+<typecode >   :  加正負號
>>> '%+f  %+f' %(100.123,-100.123 )
'+100.123000  -100.123000'

#%<Width.Precidion><typecode >   :   控制輸出位數
>>> '%6.3f  %2.2f' %(100.123,100.123 )
'100.123  100.12'


#%<0Width.Precidion><typecode >   :   控制輸出位數,不足位補'0'                                                 

>>> '%07.3f  %08.3f' %(100.123,100.123 )
'100.123  0100.123'


#%<-Width.Precidion><typecode >   :   控制輸出位數,向左靠攏

>>> '%08.3f  %-8.3f' %(100.123,100.123 )
'0100.123  100.123 '
























    『Width』: 總寬度。
    『Precidion』: 小數部份的位數。

    位數超過所設定的總寬度時,整數部份不會被截斷 ; 小數部份所設定的位數若比實際位數少,則超過的部份會被截斷。

>>> '%2.2f' %(100.125)                                               
'100.12'             



  • 配合字典控制輸出
         語法如下,

'%(key)[flags][width][.precision]typecode'    %{key:Value}         




    後面Key所對應的值會被送到前面key的位置,


>>> '%(key1)d  %(key2)f' %{"key1":123,"key2":123.321}                 
'123  123.321000'    




    b. call method 來控制字串輸出的格式

    語法如下,

'{FieldName}'.format(Value)              



      『FieldName』可以使用『位置代號』或『keyword (其實跟剛剛配合字典的方法差不多)』的對應來控制輸出。


'{Position} {Keyword}}'.format(PValue,,Keyword=KValue)




    使用『位置代號』會依序到後面取用對應的值,使用『keyword 』則取與『Keyword』所對應的值。

>>> '{0} {1}'.format('string',1.23)
'string 1.23'
>>> '{Keyword1} {Keyword2}'.format(Keyword1=3.21,Keyword2='KValue')
'3.21 KValue'
>>> '{0} {Keyword}'.format('String',Keyword='KValue')
'String KValue'









    『位置代號』可以與字典搭配使用

>>> 'Dic&Pos {0[Key]}'.format({"Key":'Value'})
'Dic&Pos Value'





    還可以使用『keyword 對應』搭配字典

>>> 'Dic&Keyword {Keyword[Key]}'.format(Keyword={"Key":'KValue'})
'Dic&Keyword KValue'




        不過新的語法可以直接省略掉『FiledName』

>>> '{} {}'.format('string',1.23)                    
'string 1.23'






      如果是序列型別的資料,可以使用索引或『Slice』的方式取用序列內的元素。

>>> List=('LVaule1','LVakue2')
>>> List
('LVaule1', 'LVakue2')

>>> Tuple=['TVaule1','TVakue2']
>>> Tuple
['TVaule1', 'TVakue2']

>>> String='AString'
>>> String
'AString'

>>> 'String={2[2]} Tuple={1[1]} List={0[0]}'.format\                                       
... (List,Tuple,String)
'String=t Tuple=TVakue2 List=LVaule1'

















    『slice』的話要先在後面處理掉

>>> 'String={2} Tuple={1} List={0}'.format \
... (List[0:1],Tuple[1:2],String[2:3])
"String=t Tuple=['TVakue2'] List=('LVaule1',)"
 
#在輸出的位置『Slice』會發生『ValueErroe』的例外
>>> 'String={2[2:3]} Tuple={1[1:2]} List={0[0:1]}'.format \
... (List,Tuple,String)
Traceback (most recent call last):
  File "", line 2, in 
ValueError: Missing ']' in format string















    如果要進一步控制輸出的位數及表示方式語法如下,


'{FieldName!ConverFlag:FormatSpec}'.foramt(<Value>)





    『ConverFlag』則用『r』、『s』、『a』表示要同時 call 『repr()』、『str()』、『ascii()』進行格式轉換。
   
    『FormatSpec』則是用來進行輸出位數、對齊方式.....等控制,語法如下,

[Align][0][Width][.Precidion][TypeCode]    


   


  『Align』用來控制向左對齊(<)、至中(^)或向右對齊(>)。

    需要不足位補『0』的話就在整數位數前加上『0』。

#只設定總寬度,預設會靠右對齊 
>>> '{0:10}.format(100.123)                     
'   100.123'
 
#未指定『TypeCode』浮點數會以指數形表示
>>> '{0:10.2}'.format(100.123)
'     1e+02'
 
#加上『TypeCode』
>>> '{0:10.2f}'.format(100.123)
'    100.12'
 
#未設定小數部份位數,浮點數的小數部份會補『0』
>>> '{0:10f}'.format(100.123)
'100.123000'
>>> '{0:010f}'.format(100.123)
'100.123000'
#要在整數部份補『0』要設定小數的位數 
>>> '{0:010.2f}'.format(100.123)
'0000100.12'
#整數的三種對齊方式 
>>> '{0:<10} {1:^10} {2:>10}'.format(66,77,88)
'66             77             88'
>>> '{0:<010} {1:^010} {2:>010}'.format(66,77,88)
'6600000000 0000770000 0000000088'
#浮點數的三種對齊方式 
>>> '{0:<10.2f} {1:^10.2f} {2:>10.2f}'.format(12.3,12.3,12.3)
'12.30        12.30         12.30'
>>> '{0:<010.2f} {1:^010.2f} {2:>010.2f}'.format(12.3,12.3,12.3)
'12.3000000 0012.30000 0000012.30'
 
#thousand-separator syntax for numbers
>>> '{0:,d}'.format(12345678)
'12,345,678'
>>> '{0:15,f}'.format(12345678.9123)
'12,345,678.912300'
>>> '{0:15,.4f}'.format(12345678.9123)
'12,345,678.9123'
 
Powered By Blogger