使用python创建一个csv文件,并将其放入嵌套列表中

使用python创建一个csv文件,并将其放入嵌套列表中,python,Python,我有一个嵌套列表,如下所示: [['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Service?'], ['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'N'], ['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'N'], ['T103', '15/11/2016', '94', '

我有一个嵌套列表,如下所示:

[['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Service?'], 
['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'N'], 
['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'N'], 
['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'Y'], 
['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'N'], 
['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Y']]
我想能够创建一个csv文件,并把在每个列表到每一行,我该怎么做?此外,每当列表更新时,我希望能够用新数据覆盖数据,我该如何做到这一点?谢谢

到目前为止,我的代码如下:

        with open(filepath + 'Bike_List_With_Service.csv','w',newline='') as file:
        a=csv.writer(file,delimiter=',')
        data=data
        a.writerows(data)#write each line of data

使用
csv
模块

import csv

rows = [['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Service?'], 
['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'N'], 
['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'N'], 
['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'Y'], 
['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'N'], 
['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Y']]

with open('output.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(rows)
输出

Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last,Service?
T101,10/4/2016,55,10/1/2017,25.08,N
T102,1/7/2016,10,15/5/2017,30.94,N
T103,15/11/2016,94,13/6/2017,83.16,Y
T104,25/4/2017,58,10/1/2017,25.08,N
T105,24/5/2017,5,20/6/2017,93.8,Y

为了能够更新现有的值(比如基于自行车号码),您首先需要在中读取文件并将数据存储在字典中。然后,您可以根据新数据更新字典条目。现在,您可以对字典中的键进行排序(如果希望文件保持排序顺序),并将更新后的数据写回您的文件。例如:

import csv

filename = 'Bike_List_With_Service.csv'
header = ['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Service?']
existing_data = {}

new_data = [
    ['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'N'], 
    ['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'N'], 
    ['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'Y'], 
    ['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'N'], 
    ['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Y']]

# try and read in existing data from the file
try:
    with open(filename, 'r', newline='') as f_input:
        csv_input = csv.reader(f_input)
        next(csv_input) # skip over existing header

        for row in csv_input:
            existing_data[row[0]] = row[1:]
except FileNotFoundErrors as e:
    pass

# update the existing data with new_data
for row in new_data:
    existing_data[row[0]] = row[1:]

# write the updated data back to the csv file    
with open(filename, 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header)

    for row in sorted(existing_data):
        csv_output.writerow([row] + existing_data[row])
这将首先尝试读取现有文件(如果该文件不存在,将继续)。dictionary键保存自行车编号,其值包含其余行值


通过使用Python的库,它负责读取一行逗号分隔的值,并自动将它们转换为每行的列表。如果值中有带引号的字符串,它也会处理这个问题。如果您不想使用库,另一种方法是读取中的每一行,删除末尾的换行符,然后使用
split()
创建列表。

我使用的是python 3.6这是csv模块中的
writerows
的基本功能。你试过了吗?请完成你的问题。显示您希望从该给定数据生成的csv文件,并进一步解释“每当列表更新时,用新数据覆盖数据”的含义。您的意思是写一个新文件还是仅仅覆盖一个更改的行?看见另外,到目前为止,您在这方面做了哪些工作?您尝试了什么?可能是@RoryDaulton的重复我想覆盖它,呃,每次我在列表中添加内容时,是否可以更新文件?
import os
        if (not os.path.isfile("/{}/Bike_List_With_Service.csv".format(filepath))):#checks if Bike_List_With_Service.csv is in path
            with open(filepath + 'Bike_List_With_Service.csv','w',newline='') as file:#creates a new csv file named 'Bikelistwithservice.csv'
                a=csv.writer(file,delimiter=',')
                data=data
                a.writerows(data)#write each line of data
        else:
            file_to_be_overwrite="Bike_List_With_Service.csv"
            ov=open(filepath+file_to_be_overwrite,"w")
            ov.truncate()       #clears everything in the file and reenter the data
            ov.close()
            with open(filepath + 'Bike_List_With_Service.csv','w',newline='') as file:#creates a new csv file named 'Bikelistwithservice.csv'
                a=csv.writer(file,delimiter='')
                data=data
                a.writerows(data)#write each line of data