-3

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
Nathan Leow
  • 83
  • 12
  • I am using python 3.6 – Nathan Leow Aug 05 '17 at 12:13
  • 1
    This is bread-and-butter for `writerows` from the csv module. Have you tried it? – roganjosh Aug 05 '17 at 12:14
  • 3
    Please complete your question. Show the csv file you want to result from that given data, and explain more what you mean by "overwrite the data with a new one whenever ,the list is updated." Do you mean write a new file or literally just overwrite the one changed line? See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also, what work have you done on this so far and what have you tried? – Rory Daulton Aug 05 '17 at 12:15
  • 1
    Possible duplicate of [Python: Write nested list objects to csv file](https://stackoverflow.com/questions/29448193/python-write-nested-list-objects-to-csv-file) – Kallz Aug 05 '17 at 12:25
  • @RoryDaulton I want to overwrite it – Nathan Leow Aug 05 '17 at 12:32
  • That does not answer my one question. What exactly is "it" that you want to "overwrite"? The entire file, or just one line in the file (not moving or copying any of the other lines)? And what is the trigger for this overwriting--is it to be done automatically whenever that data structure is changed, or when you call a function to overwrite? – Rory Daulton Aug 05 '17 at 14:00
  • The entire file ,is it better to do it with truncate or "w+"? – Nathan Leow Aug 05 '17 at 14:02

3 Answers3

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
Ricardo
  • 587
  • 5
  • 14
2

To be able to update existing values (lets say based on the bike number), you would first need to read your file in and store the data in a dictionary. You could then update the dictionary entries based on your new data. Now you can sort the keys in the dictionary (if you want the file to remain in a sorted order) and write the updated data back to your file. For example:

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])

This will first try and read an existing file (if the file does not exist it will continue). The dictionary key holds the bike number and its value contains the remaining row values.

By using Python's csv library, it takes care of reading a row of comma separated values and converting them automatically into a list per row for you. If the values had strings in them with quotes, it would deal with that too. If you are don't wish to use a library, the alternative is to read each line of the line in, remove the newline at the end and use split() to create your list.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
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
Nathan Leow
  • 83
  • 12