CSV到python,文本对齐

CSV到python,文本对齐,python,csv,alignment,Python,Csv,Alignment,所以我有一个CSV文件,它有三列。我能够读取CSV文件,表格的每一行都是一个列表。然而,当我试图像表格一样打印它们时,由于文本的长度不同,它们并没有正确对齐。我相信我必须用%来格式化它们,但我不确定如何使其工作。 我有更多的行,但出于明显的原因,我只会显示前3行 North America USA Washington Europe Republic of Ireland Dublin Asia China Beijing Thi

所以我有一个CSV文件,它有三列。我能够读取CSV文件,表格的每一行都是一个列表。然而,当我试图像表格一样打印它们时,由于文本的长度不同,它们并没有正确对齐。我相信我必须用%来格式化它们,但我不确定如何使其工作。 我有更多的行,但出于明显的原因,我只会显示前3行

North America         USA            Washington
Europe      Republic of Ireland    Dublin
Asia  China     Beijing

This is how my lists look like:
["North America"," USA", "Washington"]
["Europe"," Republic of Ireland", "Dublin"]
["Asia"," China", "Beijing"]
变成这样:

North America         USA                       Washington
Europe                Republic of Ireland       Dublin
Asia                  China                     Beijing
我不想使用prettyprint或类似的库,因为我相信这个问题的解决方案会对我更有利,这样我就可以了解事情是如何工作的。

使用 您可以使用函数(用于字符串)

ljust
将用空格(或指定的任何其他字符)从右侧填充字符串。
示例用法:

>>> 'hello'.ljust(10)
'hello     '
>>> 'hello'.ljust(10, 'A')
'helloAAAAA'
因此,在打印值时,只需使用
ljust

假设拥有
a
-包含您的值的列表列表:

for b in a:
    for c in b:
        print c.ljust(15),
    print ""
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
使用 为此,您可以使用string
format
函数:

>>> '{:10}'.format('hello')
'hello     '
标准格式说明符的一般格式为:

for b in a:
    for c in b:
        print c.ljust(15),
    print ""
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
使用 嗯,使用这种方法可能会被认为更优雅(?)或更像蟒蛇(?),但我个人并不喜欢:

>>> ('hello' + 10 * ' ')[:10]
'hello     '
使用打印格式 您可以在打印时使用百分号,用于使用C样式的printf

没关系,这不是个好主意。去别的地方看看

使用 您可以使用函数(用于字符串)

ljust
将用空格(或指定的任何其他字符)从右侧填充字符串。
示例用法:

>>> 'hello'.ljust(10)
'hello     '
>>> 'hello'.ljust(10, 'A')
'helloAAAAA'
因此,在打印值时,只需使用
ljust

假设拥有
a
-包含您的值的列表列表:

for b in a:
    for c in b:
        print c.ljust(15),
    print ""
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
使用 为此,您可以使用string
format
函数:

>>> '{:10}'.format('hello')
'hello     '
标准格式说明符的一般格式为:

for b in a:
    for c in b:
        print c.ljust(15),
    print ""
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
使用 嗯,使用这种方法可能会被认为更优雅(?)或更像蟒蛇(?),但我个人并不喜欢:

>>> ('hello' + 10 * ' ')[:10]
'hello     '
使用打印格式 您可以在打印时使用百分号,用于使用C样式的printf


没关系,这不是个好主意。去别的地方看看

可能重复使用python“\t”@Mani-tab中的选项卡仍然无法工作,因为length@JamesAdams我的回答解决了你的问题吗?可能重复使用python“\t”@Mani-tab中的选项卡仍然不起作用,因为length@JamesAdams我的回答解决了你的问题吗?