2015-01-11 28 views
5

Cho một csv với các nội dung của một cái gì đó như:tạo Bảng với dữ liệu csv

Colour, Red, Black, Blue 
Taste, Good, Bad, Disgusting 
Smell, Pleasant, Deceptive, Intolerable 

Làm thế nào tôi có thể in này ra trong python để nó sẽ trông như thế này:

+-------+-----------+-----------+ 
|Colour |Taste  | Smell  | 
+-------+-----------+-----------+ 
| Red |Good  | Pleasant | 
| Black | Bad  | Deceptive | 
| Blue | Disgusting|Intolerable| 
+-------+-----------+-----------+ 

Đỗ tôi phải tạo bảng theo cách thủ công bằng các nút của + và có tính đến các chuỗi dài nhất của cột tương ứng hoặc có phương pháp tích hợp cho điều này không? Tôi đã tìm kiếm các bảng python, nhưng không có gì liên quan đến vấn đề xuất hiện. Ngoài ra bảng ví dụ mà tôi đã nhập theo cách thủ công không phải là đối xứng trong mọi ô (không được "căn chỉnh" đúng cách).

Điểm then chốt của sự cố là + - | tạo bảng.

Việc cần làm?

Trả lời

3

Điều gần gũi nhất với một phương pháp tích hợp được sử dụng str.format:

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| 
+------------+------------+------------+ 

Nếu không biết chính xác có bao nhiêu cols là trong file:

with open("output.txt") as f: 
    lines = list(csv.reader(f, delimiter=",")) 
    mx_len = len(max((max(ele, key=len) for ele in lines), key=len)) 
    zipped = zip(*lines) 
    row1 = zipped[0] 
    ln = len(row1) 
    pattern = "-" * mx_len 
    f = (("+{pat}" * ln + "+").format(pat=pattern)) 
    print(f) 
    print(("|{:<{i}}" * ln + "|").format(*row1, i=mx_len)) 
    print(f) 
    for row in zipped[1:]: 
     print(("|{:<{i}}" * ln + "|").format(*row, i=mx_len)) 
    print(f) 

+------------+------------+------------+ 
|Colour  |Taste  |Smell  | 
+------------+------------+------------+ 
| Red  | Good  | Pleasant | 
| Black  | Bad  | Deceptive | 
| Blue  | Disgusting | Intolerable| 
+------------+------------+------------+ 
2

này không được tích hợp nhưng bạn có thể sử dụng terminaltables:

from terminaltables import AsciiTable 

with open('test.csv') as f: 
    table_data = [line.split(",") for line in f] 
    transposed = [list(i) for i in zip(*table_data)] 

print(AsciiTable(transposed).table) 

Để cài đặt chỉ làm:

pip install terminaltables 
Các vấn đề liên quan