如何漂亮在Python中打印CSV文件 [英] How to Pretty Print a CSV file in Python

查看:162
本文介绍了如何漂亮在Python中打印CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python而不是任何外部工具来打印CSV文件?

How can I pretty print a CSV file using Python, not any external tool?

例如,我有这个CSV文件:

For example I have this CSV file:

title1|title2|title3|title4
datalongdata|datalongdata|data|data

data|data|data|datalongdatadatalongdatadatalongdatadatalongdatadatalongdata
data|data'data|dat

转换它在视觉上看起来像一个表。例如,像这样:

I would like to transform it to visually look like a table. For example, to something like that:

+ --------------------------------------------------------------------------------------------------- +
| title1       | title2       | title3 | title4                                                       |
+ --------------------------------------------------------------------------------------------------- +
| datalongdata | datalongdata | data   | data                                                         |
|              |              |        |                                                              |
| data         | data         | data   | datalongdatadatalongdatadatalongdatadatalongdatadatalongdata |
| data         | data'data    | dat    |                                                              |
+ --------------------------------------------------------------------------------------------------- +


推荐答案



pretty.pretty_file(文件名,***选项*)

Usage:

pretty.pretty_file(filename, ***options*)

读取CSV文件,并将数据以表格形式可视化地打印到新文件。 filename 是给定的CSV文件。可选的***选项*关键字参数是Python的标准库 csv 的联合模块方言和格式参数以及以下列表:

Reads a CSV file and prints visually the data as table to a new file. filename, is the given CSV file. The optional ***options* keyword arguments is the union of Python's Standard Library csv module Dialects and Formatting Parameters and the following list:


  • new_delimiter :新的列分隔符(默认|)

  • border :布尔值如果您要打印表格的边框( default True)

  • |)

  • border_horizo​​ntal :表格的顶部和底部边框默认 - )

  • border_corner_tl :表格的左上角

  • border_corner_tr :表格的右上角(默认+)

  • border_corner_bl :表格的左下角 border_corner_br :表格的右下角(默认 border_corner_tr 相同)

  • header
  • : ::
  • border_header_left
  • new_delimiter: the new column separator (default " | ")
  • border: boolean value if you want to print the border of the table (default True)
  • border_vertical_left: the left border of the table (default "| ")
  • border_vertical_right: the right border of the table (default " |")
  • border_horizontal: the top and bottom border of the table (default "-")
  • border_corner_tl: the top-left corner of the table (default "+ ")
  • border_corner_tr: the top-right corner of the table (default " +")
  • border_corner_bl: the bottom-left corner of the table (default same as border_corner_tl)
  • border_corner_br: the bottom-right corner of the table (default same as border_corner_tr)
  • header: boolean value if the first row is a table header (default True)
  • border_header_separator: the border between the header and the table (default same as border_horizontal)
  • border_header_left: the left border of the table header (default same as border_corner_tl)
  • border_header_right: the right border of the table header (default same as border_corner_tr)
  • new_filename: the new file's filename (default "new_" + filename)
  • newline: defines how the rows of the table will be separated (default "\n")

示例:

import pretty_csv
pretty_csv.pretty_file("test.csv", header=False, border=False, delimiter="|")

Python 3:

。对于Python 3,您必须将 open(filename,rb)的2次出现作为输入:更改为在Python 3中, csv.reader 在文本模式下需要打开文件 p>

模组:



This is a Python 2 implementation. For Python 3, you have to change the 2 occurrences of the line open(filename, "rb") as input: to open(filename, "r", newline="") as input: since csv.reader in Python 3 wants the file to be opened in text mode.

import csv

def pretty_file(filename, **options):
    """
    @summary:
        Reads a CSV file and prints visually the data as table to a new file.
    @param filename:
        is the path to the given CSV file.
    @param **options:
        the union of Python's Standard Library csv module Dialects and Formatting Parameters and the following list:
    @param new_delimiter:
        the new column separator (default " | ")
    @param border:
        boolean value if you want to print the border of the table (default True)
    @param border_vertical_left:
        the left border of the table (default "| ")
    @param border_vertical_right:
        the right border of the table (default " |")
    @param border_horizontal:
        the top and bottom border of the table (default "-")
    @param border_corner_tl:
        the top-left corner of the table (default "+ ")
    @param border_corner_tr:
        the top-right corner of the table (default " +")
    @param border_corner_bl:
        the bottom-left corner of the table (default same as border_corner_tl)
    @param border_corner_br:
        the bottom-right corner of the table (default same as border_corner_tr)
    @param header:
        boolean value if the first row is a table header (default True)
    @param border_header_separator:
        the border between the header and the table (default same as border_horizontal)
    @param border_header_left:
        the left border of the table header (default same as border_corner_tl)
    @param border_header_right:
        the right border of the table header (default same as border_corner_tr)
    @param newline:
        defines how the rows of the table will be separated (default "\n")
    @param new_filename:
        the new file's filename (*default* "/new_" + filename)
    """

    #function specific options
    new_delimiter           = options.pop("new_delimiter", " | ")
    border                  = options.pop("border", True)
    border_vertical_left    = options.pop("border_vertical_left", "| ")
    border_vertical_right   = options.pop("border_vertical_right", " |")
    border_horizontal       = options.pop("border_horizontal", "-")
    border_corner_tl        = options.pop("border_corner_tl", "+ ")
    border_corner_tr        = options.pop("border_corner_tr", " +")
    border_corner_bl        = options.pop("border_corner_bl", border_corner_tl)
    border_corner_br        = options.pop("border_corner_br", border_corner_tr)
    header                  = options.pop("header", True)
    border_header_separator = options.pop("border_header_separator", border_horizontal)
    border_header_left      = options.pop("border_header_left", border_corner_tl)
    border_header_right     = options.pop("border_header_right", border_corner_tr)
    newline                 = options.pop("newline", "\n")

    file_path = filename.split(os.sep)
    old_filename = file_path[-1]
    new_filename            = options.pop("new_filename", "new_" + old_filename)

    column_max_width = {} #key:column number, the max width of each column
    num_rows = 0 #the number of rows

    with open(filename, "rb") as input: #parse the file and determine the width of each column
        reader=csv.reader(input, **options)
        for row in reader:
            num_rows += 1
            for col_number, column in enumerate(row):
                width = len(column)
                try:
                    if width > column_max_width[col_number]:
                        column_max_width[col_number] = width
                except KeyError:
                    column_max_width[col_number] = width

    max_columns = max(column_max_width.keys()) + 1 #the max number of columns (having rows with different number of columns is no problem)

    if max_columns > 1:
        total_length = sum(column_max_width.values()) + len(new_delimiter) * (max_columns - 1)
        left = border_vertical_left if border is True else ""
        right = border_vertical_right if border is True else ""
        left_header = border_header_left if border is True else ""
        right_header = border_header_right if border is True else ""

        with open(filename, "rb") as input:
            reader=csv.reader(input, **options)
            with open(new_filename, "w") as output:
                for row_number, row in enumerate(reader):
                    max_index = len(row) - 1
                    for index in range(max_columns):
                        if index > max_index:
                            row.append(' ' * column_max_width[index]) #append empty columns
                        else:
                            diff = column_max_width[index] - len(row[index])
                            row[index] = row[index] + ' ' * diff #append spaces to fit the max width

                    if row_number==0 and border is True: #draw top border
                        output.write(border_corner_tl + border_horizontal * total_length + border_corner_tr + newline)
                    output.write(left + new_delimiter.join(row) + right + newline) #print the new row
                    if row_number==0 and header is True: #draw header's separator
                        output.write(left_header + border_header_separator * total_length + right_header + newline)
                    if row_number==num_rows-1 and border is True: #draw bottom border
                        output.write(border_corner_bl + border_horizontal * total_length + border_corner_br)

这篇关于如何漂亮在Python中打印CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆