如何将大型字典列表转换为指定格式的文本?
实现字典列表的格式化表格输出
需求说明
你有如下存储国家信息的字典列表:
d = [{"country": "Argentina", "country_code": "AR", "slots": 207}, {"country": "Australia", "country_code": "AU", "slots": 2471}, {"country": "Austria", "country_code": "AT", "slots": 179}, {"country": "Bahrain", "country_code": "BH", "slots": 7}, {"country": "Bangladesh", "country_code": "BD", "slots": 5}, {"country": "Belarus", "country_code": "BY", "slots": 4}, {"country": "Belgium", "country_code": "BE", "slots": 58}, {"country": "Bolivia", "country_code": "BO", "slots": 3}, {"country": "Bosniaand Herzegovina", "country_code": "BA", "slots": 4}, {"country": "Brazil", "country_code": "BR", "slots": 460}, {"country": "Brunei", "country_code": "BN", "slots": 8}, {"country": "Bulgaria", "country_code": "BG", "slots": 42}, {"country": "Canada", "country_code": "CA", "slots": 639}, {"country": "Chile", "country_code": "CL", "slots": 101}, {"country": "Colombia", "country_code": "CO", "slots": 31}, {"country": "Costa Rica", "country_code": "CR", "slots": 24}, {"country": "Cyprus", "country_code": "CY", "slots": 11}, {"country": "Czechia", "country_code": "CZ", "slots": 45}, {"country": "Denmark", "country_code": "DK", "slots": 78}, {"country": "Dominican Republic", "country_code": "DO", "slots": 15}, {"country": "Ecuador", "country_code": "EC", "slots": 17}, {"country": "El Salvador", "country_code": "SV", "slots": 20}, {"country": "Estonia", "country_code": "EE", "slots": 5}, {"country": "Finland", "country_code": "FI", "slots": 31}, {"country": "France", "country_code": "FR", "slots": 1203}, {"country": "Germany", "country_code": "DE", "slots": 1149}, {"country": "Ghana", "country_code": "GH", "slots": 9}, {"country": "Greece", "country_code": "GR", "slots": 16}, {"country": "Guatemala", "country_code": "GT", "slots": 28}, {"country": "Honduras", "country_code": "HN", "slots": 15}, {"country": "Hungary", "country_code": "HU", "slots": 37}, {"country": "Iceland", "country_code": "IS", "slots": 10}, {"country": "India", "country_code": "IN", "slots": 11}, {"country": "Indonesia", "country_code": "ID", "slots": 116}, {"country": "Ireland", "country_code": "IE", "slots": 202}, {"country": "Israel", "country_code": "IL", "slots": 35}, {"country": "Italy", "country_code": "IT", "slots": 236}, {"country": "Jordan", "country_code": "JO", "slots": 5}, {"country": "Kuwait", "country_code": "KW", "slots": 9}, {"country": "Liechtenstein", "country_code": "LI", "slots": 3}, {"country": "Lithuania", "country_code": "LT", "slots": 11}, {"country": "Luxembourg", "country_code": "LU", "slots": 16}, {"country": "Malawi", "country_code": "MW", "slots": 4}, {"country": "Malaysia", "country_code": "MY", "slots": 558}, {"country": "Mexico", "country_code": "MX", "slots": 197}, {"country": "Morocco", "country_code": "MA", "slots": 8}, {"country": "Namibia", "country_code": "NA", "slots": 4}, {"country": "Netherlands", "country_code": "NL", "slots": 101}, {"country": "New Zealand", "country_code": "NZ", "slots": 90}, {"country": "Nicaragua", "country_code": "NI", "slots": 5}, {"country": "Nigeria", "country_code": "NG", "slots": 13}, {"country": "Norway", "country_code": "NO", "slots": 293}, {"country": "Pakistan", "country_code": "PK", "slots": 19}, {"country": "Panama", "country_code": "PA", "slots": 24}, {"country": "Paraguay", "country_code": "PY", "slots": 14}, {"country": "Peru", "country_code": "PE", "slots": 27}, {"country": "Philippines", "country_code": "PH", "slots": 129}, {"country": "Poland", "country_code": "PL", "slots": 184}, {"country": "Portugal", "country_code": "PT", "slots": 23}, {"country": "Romania", "country_code": "RO", "slots": 34}, {"country": "Saudi Arabia", "country_code": "SA", "slots": 6}, {"country": "Senegal", "country_code": "SN", "slots": 6}, {"country": "Serbia", "country_code": "RS", "slots": 5}, {"country": "Singapore", "country_code": "SG", "slots": 274}]
需要将其输出为带对齐分隔线的表格格式,示例如下:
Country | Country Code | Slot Argentina | AR | 207 Australia | AU | 2471 Austria | AT | 179 ...
实现方法
方法1:自适应列宽实现完美对齐
通过计算每列内容的最大宽度,让表格自动适配不同长度的内容:
# 定义表头 headers = ["Country", "Country Code", "Slot"] # 计算每列的最大宽度:表头长度和对应字段内容长度的最大值 col1_width = max(len(headers[0]), max(len(item["country"]) for item in d)) col2_width = max(len(headers[1]), max(len(item["country_code"]) for item in d)) col3_width = max(len(headers[2]), max(len(str(item["slots"])) for item in d)) # 打印表头 print(f"{headers[0].ljust(col1_width)} | {headers[1].center(col2_width)} | {headers[2].rjust(col3_width)}") # 遍历字典列表输出每行内容 for item in d: country_str = item["country"].ljust(col1_width) code_str = item["country_code"].center(col2_width) slot_str = str(item["slots"]).rjust(col3_width) print(f"{country_str} | {code_str} | {slot_str}")
方法2:固定列宽快速实现
如果对列宽有大致预期,可直接指定固定宽度简化代码:
# 打印表头 print(f"{'Country':<18} | {'Country Code':^12} | {'Slot':>5}") # 遍历输出每行 for item in d: print(f"{item['country']:<18} | {item['country_code']:^12} | {item['slots']:>5}")
输出效果
两种方法都能生成对齐美观的表格,示例片段如下:
Country | Country Code | Slot Argentina | AR | 207 Australia | AU | 2471 Austria | AT | 179 Bahrain | BH | 7 Bangladesh | BD | 5 ...
内容的提问来源于stack exchange,提问作者RexGuard
相关产品推荐
相关产品推荐

