I have a nested list like this:
[['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']]
I want to be able to create a csv file and put in each list into every line ,how do I do it ? In addition ,I want to be able to overwrite the data with a new one whenever ,the list is updated ,how can I do this? Thanks
As of now ,here is the code I have:
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
答案 0 :(得分:3)
Use the csv
module
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)
Output:
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
答案 1 :(得分:2)
为了能够更新现有值(假设基于自行车号码),您首先需要读取文件并将数据存储在字典中。然后,您可以根据新数据更新字典条目。现在,您可以对字典中的键进行排序(如果希望文件保持排序顺序),并将更新的数据写回文件。例如:
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])
这将首先尝试并读取现有文件(如果文件不存在,它将继续)。字典键保存自行车编号,其值包含剩余的行值。
通过使用Python的csv
库,它负责读取一行逗号分隔值并将它们自动转换为每行列表。如果值中带有引号的字符串,它也会处理它。如果您不想使用图书馆,另一种方法是阅读该行的每一行,删除最后一行的新行,并使用split()
创建您的列表。
答案 2 :(得分:1)
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