python - python 中的字符串格式 - 使用循环填充

标签 python loops string-formatting

我想将我的 changes 列表的数据添加到这个字符串 --> 行

我可以手动完成,这很有效,但我想循环执行。有人可以帮忙吗?

我尝试过的:

changes_dict.items {'update':[1], 'history':[0]} # this is also generated generic this is just an example

changes = {'update':["x"], 'history':["-"]} #-> this is dict with lists created generic and always changes

for key, value_list in changes_dict.items():
   row = " *  {:<13} {:<11} {:<10} {:-<1} {:-<1} {:-<1} {:-<1} {:-<1} {:-<1} {:<23}".format(
                ID,
                log_request["date"],
                log_request["author"],
                for item in changes:
                    changes[key][item]
                    
                log_request["description"])

手动是这样的:

        row = " *  {:<11} {:<11} {:<10} {:-<1} {:-<1} {:-<1} {:-<1} {:-<1} {:-<1} {:<23}".format(
            ID,
            log_request["date"],
            log_request["author"],
            changes[key][0],
            changes[key][1],
            changes[key][2],
            changes[key][3],
            changes[key][4],
            changes[key][5],
            merge_request["description"])

最佳答案

你可以试试:

*changes[key]

代替

for item in changes:
  changes[key][item]

关于python - python 中的字符串格式 - 使用循环填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65019045/

相关文章:

python - 排队大量 celery 任务

java - while 循环不起作用? java

jquery - jQuery 的each 方法中的$(this) 和$(element)

c# - 将下一个参数作为 String.Format 中的字段宽度

python - 在 python 中打印一个字符串,用偏移量左对齐

python - 如何生成 RGBColor(0x42, 0x24, 0xE9) 格式的 RGBcolor

Python-如何在for循环中将新值与先前值进行比较?

python - 我在哪里可以找到 'admin.site.urls' 的源文件?

python - 读取函数外部的文件以进行迭代

java - String.Format 进行四舍五入,无法定位非法格式转换源报错?