<p>最接近内置方法的是使用<a href="https://docs.python.org/2/library/stdtypes.html#str.format" rel="nofollow">str.format</a>:</p>
<pre><code>import csv
with open("output.txt") as f:
lines = list(csv.reader(f,delimiter=","))
# get longest string for alignment
mx_len = len(max((max(ele,key=len) for ele in lines),key=len))
# transpose the list items
zipped = zip(*lines)
# get header/first row
row1 = zipped[0]
# how many "-" we need depends on longests word length
pattern = "-"*mx_len
f = ("+{pat}+{pat}+{pat}+".format(pat=pattern))
print(f)
# pass in mx_len as align value
print("|{:<{i}}|{:<{i}}|{:<{i}}|".format(*row1,i=mx_len))
print(f)
# print the rest of the transposed data excluding column 1/row1
for a, b, c in zipped[1:]:
print("|{:<{i}}|{:<{i}}|{:<{i}}|".format(a.rstrip(),b.rstrip(),c.rstrip(),i=mx_len))
print(f)
+ + + +
|Colour |Taste |Smell |
+ + + +
| Red | Good | Pleasant |
| Black | Bad | Deceptive |
| Blue | Disgusting | Intolerable|
+ + + +
</code></pre>
<p>不知道文件中有多少COL:</p>
^{pr2}$