python打印是这样的,我希望resource_id能够对齐输出:
3种格式化方法
%格式化
>>> pi=3.141592653589793
>>> e=2.718281828459045
>>> print("pi=%.2f e=%.3f" % (pi, e) )
pi=3.14 e=2.718
“”.format()格式化
>>> pi=3.141592653589793
>>> e=2.718281828459045
>>> print("pi={:.2f} e={:.3f}".format(pi, e))
pi=3.14 e=2.718
f-string格式化
>>> pi=3.141592653589793
>>> e=2.718281828459045
>>> print(f'pi={pi:.2f} e={e:.3f}')
pi=3.14 e=2.718
占位符的使用和对齐
在占位符中指定对齐方式、宽度和精度等格式。
左对齐、右对齐和居中对齐
- 左对齐::
- 右对齐::>width
- 居中对齐::^width
name = "Alice"
formatted_string = "{:<10} is left-aligned".format(name)
print(formatted_string)
formatted_string = "{:>10} is right-aligned".format(name)
print(formatted_string)
formatted_string = "{:^10} is centered".format(name)
print(formatted_string)
指定宽度和精度
number = 123.456789
formatted_string = "{:.2f}".format(number) # 保留两位小数
print(formatted_string)
formatted_string = "{:10.2f}".format(number) # 总宽度为10,保留两位小数
print(formatted_string)
Python 的字符串格式化方法
要使 resource_id 对齐,可以使用 Python 的字符串格式化方法。以下是一个示例代码,展示如何使用字符串格式化来对齐输出:
data = [
("FieldA_2_0", 0.0, "5237aa80185eb4bc84af9d5b57c64562", "2024-06"),
("FieldA_3_0", 6913682.0, "c8f7cac5c6e85efd3690650dfcf598e6", "2024-06"),
("FieldA_4_0", 0.0, "cb5c0e991ba1bdc8ce5111a3cd50a878", "2024-06"),
("FieldA_5_0", 0.0, "gap-resource", "2024-06"),
("FieldB_0_1", 0.0, "4354", "2024-07"),
("FieldB_1_1", 27900000.0, "4769", "2024-07"),
("FieldA_0_1", 0.0, "0d9bbcf7c0f1e598d31031dca796337b", "2024-07"),
("FieldA_1_1", 0.0, "1b5f8e7771d12e3424542624805d8816", "2024-07"),
("FieldA_2_1", 0.0, "5237aa80185eb4bc84af9d5b57c64562", "2024-07"),
("FieldA_3_1", 4100000.0, "c8f7cac5c6e85efd3690650dfcf598e6", "2024-07"),
("FieldA_4_1", 0.0, "cb5c0e991ba1bdc8ce5111a3cd50a878", "2024-07"),
]
# Define the format string with fixed widths for alignment
format_str = "{:<30} = {:<10} resource_id={:<36}, month={}"
# Print each line with the specified format
for item in data:
print(format_str.format(item[0], item[1], item[2], item[3]))
使用 pandas 库来处理和格式化数据
将需要格式化的数据存储到一个列表list中(若涉及多个字段,将多个字段打包为元组再append到list中即可),然后将list转为pandas的DataFrame即可。
solver_params = pywraplp.MPSolverParameters()
solver_params.SetDoubleParam(pywraplp.MPSolverParameters.RELATIVE_MIP_GAP, 1e-8)
solver_params.SetDoubleParam(pywraplp.MPSolverParameters.PRIMAL_TOLERANCE, 1e-8)
status = solver.Solve(solver_params)
result = []
if status == pywraplp.Solver.OPTIMAL:
print("Solution:")
print("Objective value =", solver.Objective().Value())
for m in range(data["M"]):
for c in range(data["ContractPngCount"]):
result.append(
(
"FieldB_{c}_{m}",
PurchasedPng[c, m].solution_value(),
data["ContractPngId"][c],
data["MonthList"][m],
)
)
for l in range(data["ContractLngCount"]):
result.append(
(
"FieldC_{l}_{m}",
PurchasedLng[l, m].solution_value(),
data["ContractLngId"][c],
data["MonthList"][m],
)
)
for i in range(data["IncrementalPngCount"]):
result.append(
(
"FieldA_{i}_{m}",
IncrementalPngPurchased[i, m].solution_value(),
data["IncrementalPngId"][i],
data["MonthList"][m],
)
)
for j in range(data["IncrementalLngCount"]):
result.append(
(
"FieldD_{j}_{m}",
IncrementalLngPurchased[j, m].solution_value(),
data["IncrementalLngId"][i],
data["MonthList"][m],
)
)
print(
pd.DataFrame(result, columns=["Name", "Value", "Resource ID", "Month"]).to_string(
index=False
)
)
else:
print("The problem does not have an optimal solution.")