使用python在outfile中保留头文件

时间:2016-02-04 21:13:57

标签: python csv

如何在我的outfiles中创建标题或保留第一行?其他一切都很好,我只想保留我的第一线或者#34;标题"在outfiles。

#!/usr/bin/env python3
import binascii
import csv
import os.path
import sys
from tkinter.filedialog import askopenfilename, askdirectory
from tkinter.simpledialog import askinteger

def split_csv_file(f, dst_dir, keyfunc):
    csv_reader = csv.reader(f)
    csv_writers = {}
    for row in csv_reader:
        k = keyfunc(row)
        if k not in csv_writers:
            csv_writers[k] = csv.writer(open(os.path.join(dst_dir, k),
                                             mode='w', newline=''))
        csv_writers[k].writerow(row)

def get_args_from_cli():
    input_filename = sys.argv[1]
    column = int(sys.argv[2])
    dst_dir = sys.argv[3]
    return (input_filename, column, dst_dir)

def get_args_from_gui():
    input_filename = askopenfilename(
        filetypes=(('CSV', '.csv'),),
        title='Select CSV Input File')
    column = askinteger('Choose Table Column', 'Table column')
    dst_dir = askdirectory(title='Select Destination Directory')
    return (input_filename, column, dst_dir)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        input_filename, column, dst_dir = get_args_from_gui()
    elif len(sys.argv) == 4:
        input_filename, column, dst_dir = get_args_from_cli()
    else:
        raise Exception("Invalid number of arguments")
    with open(input_filename, mode='r', newline='') as f:
        split_csv_file(f, dst_dir, lambda r: r[column-1]+'.csv')
        # if the column has funky values resulting in invalid filenames
        # replace the line from above with:
        # split_csv_file(f, dst_dir, lambda r: binascii.b2a_hex(r[column-1].encode('utf-8')).decode('utf-8')+'.csv')

以下是开始CSV的示例

"<option value="""">Choose Year</option>",ParentID
"<option value=""Civic1990"">1990</option>",Civic
"<option value=""CRX1990"">1990</option>",CRX
"<option value=""Prelude1990"">1990</option>",Prelude
"<option value=""Accord1990"">1990</option>",Accord
"<option value=""Prelude1991"">1991</option>",Prelude
"<option value=""Civic1991"">1991</option>",Civic
"<option value=""CRX1991"">1991</option>",CRX
"<option value=""Accord1991"">1991</option>",Accord
"<option value=""Prelude1992"">1992</option>",Prelude
"<option value=""Civic1992"">1992</option>",Civic
"<option value=""Accord1992"">1992</option>",Accord
"<option value=""Prelude1993"">1993</option>",Prelude
"<option value=""Civic1993"">1993</option>",Civic
"<option value=""CivicdelSol1993"">1993</option>",CivicdelSol
"<option value=""Accord1993"">1993</option>",Accord
"<option value=""Passport1994"">1994</option>",Passport

OutFile将:

<option value="">Choose Year</option>
<option value="Civic1990">1990</option>
<option value="Civic1991">1991</option>
<option value="Civic1992">1992</option>
<option value="Civic1993">1993</option>
<option value="Civic1994">1994</option>
<option value="Civic1995">1995</option>
<option value="Civic1996">1996</option>
<option value="Civic1997">1997</option>
<option value="Civic1998">1998</option

OutFile将:

<option value="">Choose Year</option>
<option value="Accord1990">1990</option>
<option value="Accord1991">1991</option>
<option value="Accord1992">1992</option>
<option value="Accord1993">1993</option>
<option value="Accord1994">1994</option>
<option value="Accord1995">1995</option>
<option value="Accord1996">1996</option>
<option value="Accord1997">1997</option>
<option value="Accord1998">1998</option>

1 个答案:

答案 0 :(得分:0)

如果我正确理解了问题,您希望从正在阅读的单个csv文件中获取标题,并将其作为您正在编写的所有csv文件的标题。只需抓住标题并将其应用于您编写的每个文件。

def split_csv_file(f, dst_dir, keyfunc):
    csv_reader = csv.reader(f)
    header = next(csv_reader)
    csv_writers = {}
    for row in csv_reader:
        k = keyfunc(row)
        if k not in csv_writers:
            writer = csv.writer(open(os.path.join(dst_dir, k),
                                             mode='w', newline=''))
            writer.writerow(header)
            csv_writers[k] = writer
        csv_writers[k].writerow(row)