ch05: new chapter
This commit is contained in:
15
05-record-like/struct/metro_read.py
Normal file
15
05-record-like/struct/metro_read.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from struct import iter_unpack
|
||||
|
||||
FORMAT = 'i12s2sf' # <1>
|
||||
|
||||
def text(field: bytes) -> str: # <2>
|
||||
octets = field.split(b'\0', 1)[0] # <3>
|
||||
return octets.decode('cp437') # <4>
|
||||
|
||||
with open('metro_areas.bin', 'rb') as fp: # <5>
|
||||
data = fp.read()
|
||||
|
||||
for fields in iter_unpack(FORMAT, data): # <6>
|
||||
year, name, country, pop = fields
|
||||
place = text(name) + ', ' + text(country) # <7>
|
||||
print(f'{year}\t{place}\t{pop:,.0f}')
|
||||
Reference in New Issue
Block a user