## 翻译：《实用的Python编程》02_03_Formatting

codists 2021-02-22 16:01:31

# 2.3 格式化

`````` Name Shares Price
---------- ---------- -----------
AA 100 32.20
IBM 50 91.10
CAT 150 83.44
MSFT 200 51.23
GE 95 40.37
MSFT 50 65.10
IBM 100 70.44``````

### 字符串格式化

``````>>> name = 'IBM'
>>> shares = 100
>>> price = 91.1
>>> f'{name:>10s} {shares:>10d} {price:>10.2f}'
' IBM 100 91.10'
>>>``````

`{expression:format}` 部分会被取代。

`f-strings` 通常和 `print()` 函数一起使用：

``print(f'{name:>10s} {shares:>10d} {price:>10.2f}')``

### 格式码

``````d Decimal integer
b Binary integer
f Float as [-]m.dddddd
e Float as [-]m.dddddde+-xx
g Float, but selective use of E notation
s String
c Character (from integer)``````

``````:>10d Integer right aligned in 10-character field
:<10d Integer left aligned in 10-character field
:^10d Integer centered in 10-character field
:0.2f Float with 2 digit precision``````

### 字典格式化

``````>>> s = {
'name': 'IBM',
'shares': 100,
'price': 91.1
}
>>> '{name:>10s} {shares:10d} {price:10.2f}'.format_map(s)
' IBM 100 91.10'
>>>``````

### format()方法

``````>>> '{name:>10s} {shares:10d} {price:10.2f}'.format(name='IBM', shares=100, price=91.1)
' IBM 100 91.10'
>>> '{:10s} {:10d} {:10.2f}'.format('IBM', 100, 91.1)
' IBM 100 91.10'
>>>``````

### C 风格的格式化

``````>>> 'The value is %d' % 3
'The value is 3'
>>> '%5d %-5d %10d' % (3,4,5)
' 3 4 5'
>>> '%0.2f' % (3.1415926,)
'3.14'``````

``````>>> b'%s has %n messages' % (b'Dave', 37)
b'Dave has 37 messages'
>>>``````

## 练习

### 练习 2.8：如何格式化数字

``````>>> value = 42863.1
>>> print(value)
42863.1
>>> print(f'{value:0.4f}')
42863.1000
>>> print(f'{value:>16.2f}')
42863.10
>>> print(f'{value:<16.2f}')
42863.10
>>> print(f'{value:*>16,.2f}')
*******42,863.10
>>>``````

``````>>> print('%0.4f' % value)
42863.1000
>>> print('%16.2f' % value)
42863.10
>>>``````

``````>>> f = '%0.4f' % value
>>> f
'42863.1000'
>>>``````

### 练习 2.9：收集数据

`````` Name Shares Price Change
---------- ---------- ---------- ----------
AA 100 9.22 -22.98
IBM 50 106.28 15.18
CAT 150 35.46 -47.98
MSFT 200 20.89 -30.34
GE 95 13.48 -26.89
MSFT 50 20.89 -44.21
IBM 100 106.28 35.84``````

`make_report()` 函数添加到 `report.py` 文件中。如果交互式地执行该函数，则应该按以下步骤进行：

``````>>> portfolio = read_portfolio('Data/portfolio.csv')
>>> report = make_report(portfolio, prices)
>>> for r in report:
print(r)
('AA', 100, 9.22, -22.980000000000004)
('IBM', 50, 106.28, 15.180000000000007)
('CAT', 150, 35.46, -47.98)
('MSFT', 200, 20.89, -30.339999999999996)
('GE', 95, 13.48, -26.889999999999997)
...
>>>``````

### 练习 2.10：打印格式化的表格

``````>>> for r in report:
print('%10s %10d %10.2f %10.2f' % r)
AA 100 9.22 -22.98
IBM 50 106.28 15.18
CAT 150 35.46 -47.98
MSFT 200 20.89 -30.34
...
>>>``````

``````>>> for name, shares, price, change in report:
print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}')
AA 100 9.22 -22.98
IBM 50 106.28 15.18
CAT 150 35.46 -47.98
MSFT 200 20.89 -30.34
...
>>>``````

### 练习 2.11：添加标题

``headers = ('Name', 'Shares', 'Price', 'Change')``

``' Name Shares Price Change'``

``'---------- ---------- ---------- -----------'``

`````` Name Shares Price Change
---------- ---------- ---------- ----------
AA 100 9.22 -22.98
IBM 50 106.28 15.18
CAT 150 35.46 -47.98
MSFT 200 20.89 -30.34
GE 95 13.48 -26.89
MSFT 50 20.89 -44.21
IBM 100 106.28 35.84``````

### 练习 2.12：格式化挑战

`````` Name Shares Price Change
---------- ---------- ---------- ----------
AA 100 \$9.22 -22.98
IBM 50 \$106.28 15.18
CAT 150 \$35.46 -47.98
MSFT 200 \$20.89 -30.34
GE 95 \$13.48 -26.89
MSFT 50 \$20.89 -44.21
IBM 100 \$106.28 35.84``````

https://segmentfault.com/a/1190000039253033