将2D数组保存到CSV

时间:2019-10-24 15:07:50

标签: python csv save

你好,我想功能输入是列表的,每个列表另存为1行

我试图保存数组2d

my_list = [['\ufeffUser Name', 'First Name', 'Last Name', 'Display Name', 'Job Title', 'Department', 'Office Number', 'Office Phone', 'Mobile Phone', 'Fax', 'Address', 'City', 'State or Province', 'ZIP or Postal Code', 'Country or Region'], ['chris@contoso.com', 'Chris', 'Green', 'Chris Green', 'IT Manager', 'Information Technology', '123451', '123-555-1211', '123-555-6641', '123-555-9821', '1 Microsoft way', 'Redmond', 'Wa',
'98052', 'United States'], ['ben@contoso.com', 'Ben', 'Andrews', 'Ben Andrews', 'IT Manager', 'Information Technology', '123452', '123-555-1212', '123-555-6642', '123-555-9822', '1 Microsoft way', 'Redmond', 'Wa', '98052', 'United States'], ['david@contoso.com', 'David', 'Longmuir', 'David Longmuir', 'IT Manager', 'Information Technology', '123453', '123-555-1213', '123-555-6643', '123-555-9823', '1 Microsoft way', 'Redmond', 'Wa', '98052', 'United States'], ['cynthia@contoso.com', 'Cynthia', 'Carey', 'Cynthia Carey', 'IT Manager', 'Information Technology', '123454', '123-555-1214', '123-555-6644', '123-555-9824', '1 Microsoft way', 'Redmond', 'Wa', '98052', 'United States'], ['melissa@contoso.com', 'Melissa', 'MacBeth', 'Melissa MacBeth', 'IT Manager', 'Information Technology', '123455', '123-555-1215', '123-555-6645', '123-555-9825', '1 Microsoft way', 'Redmond', 'Wa', '98052', 'United States']]

unifile.dump.excel("file.csv", "array", "utf-8", my_list)

我试图将列表保存到csv

l1 = ["aa", "ftyg"]
l2 = ["fgghg", "ftyfuv"]

unifile.dump.excel("file.csv", "list", "utf-8", l1, l2)

我的功能

def excel(file_path: str,  mode: str = "array",  t_encoding: str = "utf-8", write_mode: str = "r", *data: list):

    try:
        os.remove(file_path)
    except:
        pass
    if mode == "list":
        with open(file_path, 'w') as csv_file:
            csv_writer = csv.writer(csv_file, delimiter=',')
            for l in data:
                csv_writer.writerow(l)

    elif mode == "array":
        with open(file_path, write_mode, encoding=t_encoding) as csv_file:
            csv_writer = csv.writer(csv_file, delimiter=',')
            for l in data:
                csv_writer.writerow(l)
    else:
        pass
User Name,First Name,Last Name,Display Name,Job Title,Department,Office Number,Office Phone,Mobile Phone,Fax,Address,City,State or Province,ZIP or Postal Code,Country or Region
chris@contoso.com,Chris,Green,Chris Green,IT Manager,Information Technology,123451,123-555-1211,123-555-6641,123-555-9821,1 Microsoft way,Redmond,Wa,98052,United States
ben@contoso.com,Ben,Andrews,Ben Andrews,IT Manager,Information Technology,123452,123-555-1212,123-555-6642,123-555-9822,1 Microsoft way,Redmond,Wa,98052,United States
david@contoso.com,David,Longmuir,David Longmuir,IT Manager,Information Technology,123453,123-555-1213,123-555-6643,123-555-9823,1 Microsoft way,Redmond,Wa,98052,United States
cynthia@contoso.com,Cynthia,Carey,Cynthia Carey,IT Manager,Information Technology,123454,123-555-1214,123-555-6644,123-555-9824,1 Microsoft way,Redmond,Wa,98052,United States
melissa@contoso.com,Melissa,MacBeth,Melissa MacBeth,IT Manager,Information Technology,123455,123-555-1215,123-555-6645,123-555-9825,1 Microsoft way,Redmond,Wa,98052,United States

问题在于此代码将用结束行替换所有先前的行。

1 个答案:

答案 0 :(得分:0)

pandas模块非常有用。这对我有用:

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

希望有帮助