简体   繁体   中英

How do I write to a CSV in Python, and have the new data below the last line?

I have a CSV file with all my vinyls and I am writing a programme to open the CSV file then ask the user for details of a new LP and then the programme should writes the data to the CSV file. What is happening is that the data is being written to the right of the last record in the CSV file NOT as a new line below the last row. This is my code:

from csv import writer

def append_list_as_row(file_name, list_of_elem):
    with open("MusicDatacopy.csv", 'a+', newline='') as write_obj:
        
        csv_writer = writer(write_obj)
        csv_writer.writerow(list_of_elem)

artist = input("Enter Artist name: ").title()
album = input("Enter Album name: ").title()
month = input("Enter month bought: ").title()
owner = input("Enter Name of Owner: ").title()
f_name = input("Enter artist f_name: ").title()
s_name = input("Enter artist f_name: ").title()
genre = input("Enter genre: ").title()

row_contents = [artist, album, month, owner, f_name, s_name, genre]
append_list_as_row('MusicDatacopy.csv', row_contents)

Is there a way to force the CSV writer to write the new data underneath the last row of data and then sort the CSV file based on surname of artist (s_name)?

The clue is you say you want to leave your CSV in a sorted state. To do that will involve first reading the whole file in. Adding your new information, sorting it based on the surname (and possibly then on the firstname). Finally writing the whole thing back.

I am assuming your file has a header. In which case you need to take care not to include that as part of the sort.

For example:

import csv

def append_list_as_row(file_name, row):
    filename = "MusicDatacopy.csv"

    # Read the existing file in
    with open(filename, newline='') as f_input:
        csv_reader = csv.reader(f_input)
        header = next(csv_reader)
        data = list(csv_reader)
        
    data.append(row)    # add the new row to the end
    data.sort(key=lambda x: (x[5], x[4]))    # sort the data based on s_name

    # Write the whole file back
    with open(filename, 'w', newline='') as f_output:
        csv_writer = csv.writer(f_output)
        csv_writer.writerow(header)
        csv_writer.writerows(data)


artist = input("Enter Artist name: ").title()
album = input("Enter Album name: ").title()
month = input("Enter month bought: ").title()
owner = input("Enter Name of Owner: ").title()
f_name = input("Enter artist f_name: ").title()
s_name = input("Enter artist s_name: ").title()
genre = input("Enter genre: ").title()

row_contents = [artist, album, month, owner, f_name, s_name, genre]
append_list_as_row('MusicDatacopy.csv', row_contents)

Try this:

import pandas as pd

df = pd.read_csv("MusicDatacopy.csv")

artist = input("Enter Artist name: ").title()
album = input("Enter Album name: ").title()
month = input("Enter month bought: ").title()
owner = input("Enter Name of Owner: ").title()
f_name = input("Enter artist f_name: ").title()
s_name = input("Enter artist s_name: ").title()
genre = input("Enter genre: ").title()

columns = df.columns
df2 = pd.DataFrame([[artist, album, month, owner, owner, f_name, s_name, genre]], columns=columns)
df.append(df2, ignore_index=True)
df.sort_values(["s_name", "f_name"], inplace=True)
df.to_csv('MusicDatacopy.csv', index=False)

PS I haven't tested it, lemme know if you come across an issue. PS Make sure to create the list in the same order as the columns appear in csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM