Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python'中添加动态大括号{};s字符串格式_Python_Python 3.x - Fatal编程技术网

如何在python'中添加动态大括号{};s字符串格式

如何在python'中添加动态大括号{};s字符串格式,python,python-3.x,Python,Python 3.x,我有一个列表,其中包含多个列表作为列。问题是列的数量不是固定的,可能有一列、两列或更多列。 我希望能够打印列表,如下所示: print('{}->dynamically add more to contain the variables'.format(columns_list[0] ->to however many there are])) 这将在某种循环中使用 import csv rows_amount = 0 columns_amount = 0 stop = 0 wi

我有一个列表,其中包含多个列表作为列。问题是列的数量不是固定的,可能有一列、两列或更多列。 我希望能够打印列表,如下所示:

print('{}->dynamically add more to contain the variables'.format(columns_list[0] ->to however many there are]))
这将在某种循环中使用

import csv

rows_amount = 0
columns_amount = 0
stop = 0
with open('./new/new-page-1-table-1.csv', 'r') as csv_file:
    reader = list(csv.reader(csv_file))

    for row in reader:
        columns_amount = len(row)
        #In this case, the column_amount = 2 
在上面的这个例子中,列amount=2如果我想打印它,我会明确地写出来

print ('{}, {}'.format(row[0], row[1]))
如果我想在多个csv文件上运行,有没有办法动态打印所有列


有什么方法可以做到这一点吗?

根据您想要格式化的字符串,如果您只想将列表条目分隔开,您可以这样做

print(", ".join(columns_list))
使用
join()。
它具有良好的运行时行为并处理尾随/前导分隔符

如果您的格式需要formatstring,尽管第一个选项通常是首选的,尤其是在可读性方面,您可以这样处理:

print(("{} is a column! " * len(columns_list)).format(*columns_list))
您可以将这样的需求混合在一起,使其像

print(", ".join(["{:<10}".format(col) for col in columns_list]))

print(“,”).join([“{:根据您想要格式化的字符串,如果您只想将列表项分隔开,您可以这样做

print(", ".join(columns_list))
使用
join()。
它具有良好的运行时行为并处理尾随/前导分隔符

如果您的格式需要formatstring,尽管第一个选项通常是首选的,尤其是在可读性方面,您可以这样处理:

print(("{} is a column! " * len(columns_list)).format(*columns_list))
您可以将这样的需求混合在一起,使其像

print(", ".join(["{:<10}".format(col) for col in columns_list]))

print(“,”).join([“{:您所说的列是什么意思?一个示例输入和预期输出会很有帮助。将添加更多详细信息更多详细信息会很好,但可能只是
”->“。join(列)
?为什么join不能实现这一点?这看起来与
str.join
的目的完全一样……这回答了你的问题吗?列是什么意思?输入和预期输出的示例会很有帮助。将添加更多详细信息更多的详细信息会很好,但可能只是
“->”。join(列)
?为什么join不能做到这一点?这看起来和str.join的用途一模一样……这是否回答了您的问题?