您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-在对齐列中打印CSV字符串列表

Python-在对齐列中打印CSV字符串列表

import csv
from StringIO import StringIO

rows = list(csv.reader(StringIO(
    '''value1,somevalue2,value3,reallylongvalue4,value5,superlongvalue6
value1,value2,reallylongvalue3,value4,value5,somevalue6''')))

widths = [max(len(row[i]) for row in rows) for i in range(len(rows[0]))]

for row in rows:
    print(' | '.join(cell.ljust(width) for cell, width in zip(row, widths)))

输出

value1 | somevalue2 | value3           | reallylongvalue4 | value5 | superlongvalue6
value1 | value2     | reallylongvalue3 | value4           | value5 | somevalue6
python 2022/1/1 18:47:12 有524人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶