如何将标题添加到我的列表(csv)

时间:2016-04-13 16:00:33

标签: python csv

我是python的新手,我决定使用csv,我不知道如何为每个列添加标题。任何帮助将不胜感激。

def Option_B():
import csv
print("Estimate Number","Date","Customer_ID","Final_Total","Status","Amount_Paid")
with open("paintingJobs.txt") as f:
    reader = csv.reader(f, delimiter=',')
    for columns in reader: 
        if "A" in columns and int(columns[5]) < int(columns[3]):
            print (columns)

这是我想要的结果

 title 1   title 2       3       4      5    6
['E5345', '22/09/2015', 'C106', '815', 'A', '400']
['E5348', '23/09/2015', 'C109', '370', 'A', '200']
['E5349', '25/09/2015', 'C110', '480', 'A', '250']
['E5353', '28/09/2015', 'C114', '272', 'A', '200']
['E5355', '29/09/2015', 'C116', '530', 'A', '450']
['E5363', '01/10/2015', 'C124', '930', 'A', '500']
['E5364', '02/10/2015', 'C125', '915', 'A', '800']
['E5367', '03/10/2015', 'C128', '427', 'A', '350']
['E5373', '10/10/2015', 'C134', '1023', 'A', '550'] 

1 个答案:

答案 0 :(得分:0)

要执行您想要的操作,需要两次处理csv文件的所有行,以确定每列中项目的最大宽度,以便正确对齐列标题。因此,在下面的代码中,文件被读取两次。当然,将整个文件读入内存会更快,如果它不是太大,因为磁盘I / O比访问已经存储在内存中的数据慢得多。

import csv

FILE_NAME = "paintingJobs.txt"
COL_HEADERS = ['title 1', 'title 2', '3', '4', '5', '6']
NUM_COLS = len(COL_HEADERS)

# read file once to determine maximum width of data in columns
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # determine the maximum width of the data in each column
    max_col_widths = [len(col_header) for col_header in COL_HEADERS]
    for columns in reader:
        if "A" in columns and int(columns[5]) < int(columns[3]):
            for i, col in enumerate(columns):
                    max_col_widths[i] = max(max_col_widths[i], len(repr(col)))
    # add 1 to each for commas
    max_col_widths = [col_width+1 for col_width in max_col_widths]

# read file second time to display its contents with the headers
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # display justified column headers
    print(' ' + ' '.join(col_header.ljust(max_col_widths[i])
                            for i, col_header in enumerate(COL_HEADERS)))
    # display column data
    for columns in reader:
        if "A" in columns and int(columns[5]) < int(columns[3]):
            print(columns)

输出:

 title 1  title 2       3       4       5    6
['E5345', '22/09/2015', 'C106', '815', 'A', '400']
['E5348', '23/09/2015', 'C109', '370', 'A', '200']
['E5349', '25/09/2015', 'C110', '480', 'A', '250']
['E5353', '28/09/2015', 'C114', '272', 'A', '200']
['E5355', '29/09/2015', 'C116', '530', 'A', '450']
['E5363', '01/10/2015', 'C124', '930', 'A', '500']
['E5364', '02/10/2015', 'C125', '915', 'A', '800']
['E5367', '03/10/2015', 'C128', '427', 'A', '350']
['E5373', '10/10/2015', 'C134', '1023', 'A', '550']

注意:为了便于携带,使用csv模块读取(和写入)csv文件应该处于二进制模式,以便操作系统区分它和Windows之类的“文本”模式确实。要在Python 2中以二进制模式打开文件,请使用open('filename', 'rb'),在Python 3中使用open('filename', 'r', newline='')。打开要写入的文件是将r替换为w

另请注意在计算列中每个项目的宽度时使用len(repr(col))。这是因为columns是一个列表而print(columns)显示列表中每个项目的表示而不仅仅是它的值(即对于字符串,它是获取之间的区别'E5345'E5345)。